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.