0

i have a little problem here, there is no error but its not working, i have a LogIn which is connected to the MS ACCESS, the ms access contains the User and Password here is the part of the code that contains problem:

'Get Password from database
            Dim dbpassword As String = dt.Rows(0).Item("Password")
            'compare password from database to the input password


            If password.CompareTo("dbpassword") = 0 Then
                MsgBox("Successful!, Correct Password", MsgBoxStyle.OkOnly, "Correct")
                Me.Close()
                Form2.Show()

            Else
                MsgBox("Wrong Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
            End If

            'If wrong username
        Catch ex As Exception
            MsgBox("Invalid Input!, Invalid Entry", MsgBoxStyle.OkOnly, "Invalid")
            Me.TextBox1.Text = ""
            Me.TextBox2.Text = ""

        End Try

    End If
End Sub

The problem is its going right through the Catch ex AS Exception and the Msgbox "Invalid Input" will prompt even if the user and password is typed correctly. the If password.CompareTo is not working. Here is my full code:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'Declare Variables 
    Dim connection As New OleDbConnection
    Dim username As String
    Dim password As String

    'Get username and password input
    username = Me.TextBox1.Text.ToString
    password = Me.TextBox2.Text.ToString

    'Check username if NULL
    If username = "" Then
        MsgBox("Please Enter Username!, Invalid UserName", MsgBoxStyle.OkOnly, "Invalid")
        Me.TextBox2.Text = ""

        'Check password if NULL
    ElseIf password = "" Then
        MsgBox("Please Enter Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
        Me.TextBox1.Text = ""
    Else
        Try

            'Establish database connection
            connection = New OleDb.OleDbConnection
            connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;; Data Source=" & Application.StartupPath & "\Data.mdb"
            connection.Open()

            'Input Query
            Dim da As New OleDb.OleDbDataAdapter("Select * FROM tblUser WHERE User =" & username, connection)
            Dim dt As New DataTable

            'Fill data table
            da.Fill(dt)

            'Get Password from database
            Dim dbpassword As String = dt.Rows(0).Item("Password")
            'compare password from database to the input password


            If password.CompareTo("dbpassword") = 0 Then
                MsgBox("Successful!, Correct Password", MsgBoxStyle.OkOnly, "Correct")
                Me.Close()
                Form2.Show()

            Else
                MsgBox("Wrong Password!, Invalid Password", MsgBoxStyle.OkOnly, "Invalid")
            End If

            'If wrong username
        Catch ex As Exception
            MsgBox("Invalid Input!, Invalid Entry", MsgBoxStyle.OkOnly, "Invalid")
            Me.TextBox1.Text = ""
            Me.TextBox2.Text = ""

        End Try

    End If
End Sub

Thankyou in advance.

EysAce
  • 17
  • 5
  • 12
  • You are comparing string to literal "dbpassword" which is enclosed in double quotation instead the value of variable dbpassword, remove the double quotation – Jade Dec 17 '13 at 05:21
  • @Jade Still get the same problem sir. – EysAce Dec 17 '13 at 05:26
  • try to add a break point here [Dim dbpassword As String = dt.Rows(0).Item("Password")] by pressing F9 and see stop there, if then press F8. this way you will see the execution line by line before it jumps to catch area. – Jade Dec 17 '13 at 05:36

3 Answers3

0

try changing

password.CompareTo("dbpassword")

to

password.CompareTo(dbpassword)
Jade
  • 2,972
  • 1
  • 11
  • 9
0
da.Fill(dt)
If Not dt Is Nothing Then
'Get Password from database
Dim dbpassword As String = dt.Rows(0).Item("Password")
If password.Equals(dbpassword)Then
'Do something
End If
End IF

first check dt has value and check it's equal value

senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
0

WOULD YOU UPDATE YOUR CATCH code to something like

Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.OkOnly, "Invalid")

so we can get the actual exception and then post the exception

Jade
  • 2,972
  • 1
  • 11
  • 9