Okay, here is one method which for this demo I simply created two TextBox controls and seeded them with correct user name and password, result, they are allowed to enter, next I seeded user name with a bad name, not llowed,to enter, same with password. So the idea I am getting at is seed each TextBox then run the query.
Seeding at the start of the procedure
Code:
txt_Username.Text = "KnownName"
txt_Password.Text = "Password for user"
Here is what I would suggest which will work as expected, succeed or fail based on data used in the select statement.
Code:
Private txt_Username As New TextBox With {.Text = "KSG"}
Private txt_Password As New TextBox With {.Text = "pass1"}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")
}
If String.IsNullOrWhiteSpace(txt_Username.Text) Then
MessageBox.Show("Please enter Username", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
If String.IsNullOrWhiteSpace(txt_Password.Text) Then
MessageBox.Show("Please enter Password.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Using con As New OleDbConnection(Builder.ConnectionString)
Using cmd As New OleDbCommand("SELECT username,password FROM login WHERE username = ? AND password = ?", con)
cmd.Parameters.AddWithValue("username", txt_Username.Text.Trim)
cmd.Parameters.AddWithValue("password", txt_Password.Text.Trim)
Try
con.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader
If Not reader.HasRows Then
MessageBox.Show("Login Failed." & Environment.NewLine & _
"Enter Correct Username and/or Password", _
Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Allowed to enter")
End If
Catch ex As Exception
MessageBox.Show(ex.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Using
End Using
End Sub