0

my login code works fine but the it gives me problems when the username and password in questin are not on the first row in the datatable. This is because when the code in debug mode, I took the time to check the contents of the datatable using the datatable visualiser and I actually noticed that the username and password are there in the datatable but are not just in the first position , what can I do to correct this error.

                Dim STRsql As String = "SELECT  reference,UserName,UserPassword,UserCompany FROM Users WHERE UserName = '" & cmbUsername.Text & "'  "
    'Conn.Open()
    Dim dTable As DataTable = New DataTable
    Dim dscmd As New OleDbDataAdapter(STRsql, Conn)
    dscmd.Fill(dTable)
    Conn.Close()


    If dTable.Rows(0)("UserName") = cmbUsername.Text Then

        If dTable.Rows(0)("UserPassword") = txtPassword.Text Then

            comm1.LogINUserName = dTable.Rows(0)("UserName").ToString
            'MsgBox(comm1.LogINUserName)
            frmMenu.Show()
        Else
            MsgBox("Wrong Password and/or Username") '***In the case of wrong Password
            Exit Sub
        End If


    Else
        MsgBox("Wrong Password and/or Username") '***In the case of wrong UserName
        Exit Sub
    End If
2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by poojavb
1

But shud I ask u y do u need a datatable if u can login without it???

check my below code

 Try
                Dim myCommand As OleDbCommand
                myCommand = New OleDbCommand("SELECT * FROM userlogin where UserName='" & txtUsername.Text & "' and Password='" + txtPassword.Text + "'", Connection)
                Dim reader As OleDbDataReader = myCommand.ExecuteReader
                If reader.Read = True Then
                    If String.Compare(txtUsername.Text, reader("UserName"), False) Or String.Compare(txtPassword.Text, reader("Password"), False) Then 'for case sensitivity
                        MsgBox("Incorrect Credentials")
                        txtPassword.Text = ""
                        txtUsername.Text = ""
                        txtUsername.Focus()
                    Else
                        frmMain.lblWelcome.Text = "Welcome, " + reader("DrName") 'show the main form with the User First Name
                        frmMain.Show()
                        Me.Close()
                    End If
                Else
                    MsgBox("Login Failed. Try Again")
                    txtPassword.Text = ""
                    txtUsername.Text = ""
                    txtUsername.Focus()
                End If
                    reader.Close()
            Catch ex As Exception
                MsgBox("Error Connecting to Database: " & ex.Message)
            End Try

Check if this helps u....

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.