Rafael Diaz Rafael Diaz - 7 months ago 11
SQL Question

Validating Username And Password from MySQL

I'm looking for a code that validates the username and password in VB.

Private Sub LoginBtn_Click(sender As Object, e As EventArgs) Handles LoginBtn.Click
Try
con.Open()
com = New MySqlCommand("SELECT id FROM accounts WHERE uname='" & txtuname.Text & "'", con)
reader = com.ExecuteReader
If reader.HasRows = True Then
com = New MySqlCommand("SELECT id FROM accounts WHERE pword = '" & txtpword.Text & "' AND uname='" & txtuname.Text & "'", con)
If reader.HasRows = True Then
Form3.Show()
Else
MsgBox("Invalid Password")
txtpword.Focus()
End If

ElseIf String.IsNullOrEmpty(txtuname.Text) Then
MsgBox("Invalid Username")
txtuname.Focus()
Else
MsgBox("New User Detected. Input Password")
Form2.Show()
End If

reader.Close()
con.Close()

Catch ex As Exception
If Not con.State = ConnectionState.Closed Then
con.Close()
End If
MsgBox(ex.ToString)
End Try

End Sub


The conditions goes like this:


  1. If the username is not detected from the dbase, it'll lead to a registration form(which is form2)

  2. If the username was detected but the password is wrong or empty, a msgbox will pop up

  3. If the username and password is both correct, it'll lead to form 3.



My problem is condition #2. Even though the data in the pword txtbox is wrong, it still keeps leading to form 3 instead of showing a msgbox.

Answer

You should have to call again

reader = com.ExecuteReader

After:

com = New MySqlCommand("SELECT id FROM accounts WHERE pword = '" & txtpword.Text & "' AND uname='" & txtuname.Text & "'", con)

You have just checked it has rows before and you have not changed it.

Comments