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

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....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.