Results 1 to 2 of 2

Thread: [2005] Need help with login

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2005
    Posts
    87

    Post [2005] Need help with login

    I have a registration page wich is working fine, so I can add user. The problem is the login page, it alwais says that the login is invalid and I cant figure out what is wrong. I am using mysql.

    Here is the login code:

    Imports:
    VB Code:
    1. Imports System.Web.Security
    2. Imports System.Configuration
    3. Imports MySql.Data.MySqlClient
    4. Imports MySql.Data
    5.  
    6. Imports System.Data.OleDb

    Function:
    VB Code:
    1. Partial Class login2
    2.     Inherits System.Web.UI.Page
    3.     Public storecons As String
    4.     Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
    5.         storecons = _
    6.         "SELECT COUNT(*) AS Num_of_User " & _
    7.         "FROM tblUser " & _
    8.         "WHERE U_Name = @UserName AND U_Password = @Password "
    9.  
    10.         Dim MyConn As MySqlConnection = New MySqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
    11.         Dim MyCmd As New MySqlCommand(storecons, MyConn)
    12.         MyCmd.CommandType = Data.CommandType.Text
    13.         Dim objParam1, objParam2 As MySqlParameter
    14.  
    15.         objParam1 = MyCmd.Parameters.Add("@UserName", MySqlDbType.VarChar)
    16.         objParam2 = MyCmd.Parameters.Add("@Password", MySqlDbType.VarChar)
    17.  
    18.         objParam1.Direction = Data.ParameterDirection.Input
    19.         objParam2.Direction = Data.ParameterDirection.Input
    20.  
    21.         objParam1.Value = txtUserName.Text
    22.         objParam2.Value = txtPassword.Text
    23.  
    24.         Dim objReader As OleDbDataReader
    25.  
    26.         '   |||||   Try, catch block!
    27.         Try
    28.             '   |||||   Check if Connection to DB is already open, if not, then open a connection
    29.             If MyConn.State = Data.ConnectionState.Closed Then
    30.                 '   |||||   DB not already Open...so open it
    31.                 MyConn.Open()
    32.             End If
    33.  
    34.             '   |||||   Create OleDb Data Reader
    35.  
    36.             objReader = MyCmd.ExecuteScalar(Data.CommandBehavior.CloseConnection)
    37.             '   |||||   Close the Reader and the Connection Closes with it
    38.  
    39.             While objReader.Read()
    40.                 If CStr(objReader.GetValue(0)) <> "1" Then
    41.                     lblMessage.Text = "Invalid Login!"
    42.                 Else
    43.                     objReader.Close()   '   |||||   Close the Connections & Reader
    44.                     Return True
    45.                 End If
    46.             End While
    47.         Catch ex As Exception
    48.             lblMessage.Text = "Error Connecting to Database!"
    49.         End Try
    50.     End Function

    Login button:
    VB Code:
    1. Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
    2.         If Page.IsValid Then    '   ||||| Meaning the Control Validation was successful!
    3.             '   |||||   Connect to Database for User Validation |||||
    4.             If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
    5.                 FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)  '   |||||   default.aspx Page!
    6.                 Session("User") = txtUserName.Text
    7.             Else
    8.                 '   |||||   Credentials are Invalid
    9.                 lblMessage.Text = "Invalid Login!"
    10.             End If
    11.         End If
    12.     End Sub

    I hope someone can help me!

    Thanks!

  2. #2
    Addicted Member rabid lemming's Avatar
    Join Date
    Feb 2005
    Posts
    210

    Smile Re: [2005] Need help with login

    Ok Well first off your call to the class code can only return a trure or false value but in the class code you have lines where your returning a string. Try this:

    Code:
    Partial Class login2
        Inherits System.Web.UI.Page
        Public storecons As String
        Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
            storecons = _
            "SELECT COUNT(*) AS Num_of_User " & _
            "FROM tblUser " & _
            "WHERE U_Name = @UserName AND U_Password = @Password "
    
            Dim MyConn As MySqlConnection = New MySqlConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
            Dim MyCmd As New MySqlCommand(storecons, MyConn)
            MyCmd.CommandType = Data.CommandType.Text
            Dim objParam1, objParam2 As MySqlParameter
    
            objParam1 = MyCmd.Parameters.Add("@UserName", MySqlDbType.VarChar)
            objParam2 = MyCmd.Parameters.Add("@Password", MySqlDbType.VarChar)
    
            objParam1.Direction = Data.ParameterDirection.Input
            objParam2.Direction = Data.ParameterDirection.Input
    
            objParam1.Value = txtUserName.Text
            objParam2.Value = txtPassword.Text
    
            Dim objReader As OleDbDataReader
    
            '   |||||   Try, catch block!
            Try
                '   |||||   Check if Connection to DB is already open, if not, then open a connection
                If MyConn.State = Data.ConnectionState.Closed Then
                    '   |||||   DB not already Open...so open it
                    MyConn.Open()
                Else
                    msgbox("Could Not Open data base")
                    Return False
                End If
    
                '   |||||   Create OleDb Data Reader
                objReader = MyCmd.ExecuteScalar(Data.CommandBehavior.CloseConnection)
                '   |||||   Close the Reader and the Connection Closes with it
    
                While objReader.Read()
    
                    If CStr(objReader.GetValue(0)).toString() <> "1" Then
                        Return False
                    Else
                        objReader.Close()   '   |||||   Close the Connections & Reader
                        Return True
                    End If
                End While
            Catch ex As Exception
                msgbox(ex.Message.ToString())
                Return False
            End Try
        End Function
    End Class
    And on the login page:

    Code:
    Private Sub cmdSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
            If Page.IsValid Then    '   ||||| Meaning the Control Validation was successful!
                '   |||||   Connect to Database for User Validation |||||
                If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) = True Then
                    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)  '   |||||   default.aspx Page!
                    Session("User") = txtUserName.Text
                Else
                    '   |||||   Credentials are Invalid
                    lblMessage.Text = "Invalid Login!"
                End If
            End If
        End Sub
    That should work but its hard to be sure with out been able to test it...should that fail to work try tracing the line: CStr(objReader.GetValue(0)).toString() to see what's happening their i.e.

    'Add After Line While objReader.Read()
    msgbox(CStr(objReader.GetValue(0)).toString())

    I hope that helps a bit
    Last edited by rabid lemming; Jun 27th, 2006 at 05:15 AM.

    I will wait for death with a smile and a big stick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width