hey everyone. I need you experties help to the correct this login form code. After the best effort I find this code. But it occur one error.
Before the explain error I'll describe how this work. This login form check user name and password from the mysql database. After Normaliy user can login, but in my case user name and password match doesn't allow login. It check two more things.
- User baned or active ? If baned message box show and say "Please pay your fees" If user active then check second.
2.User type. If admin then load admin home page. If user (type) then load normal user home page.
I'll put my login button code below.
Try
If txtpassword.Text = "" Or txtusername.Text = "" Then
MessageBox.Show("Please Fill in Empty TextBox!")
Else
Try
con.Open()
Dim query As String
query = "SELECT User_Type,User_Status FROM moneycs.user where User_Name= '" & txtusername.Text & "' and Password = '" & txtpassword.Text & "' "
cmd = New MySqlCommand(query, con)
dr = cmd.ExecuteReader
Dim count As Integer
count = 0
While dr.Read
count = count + 1
End While
Dim userstatus = dr.GetString("User_Status")
If userstatus = "Active" Then
If count = 1 Then
Dim usertype = dr.GetString("User_Type")
If usertype = "Admin" Then
'MessageBox.Show("Your Login As Admin", "Welcome Admin !")
For j = 0 To 500
Me.Hide()
frmmlsadmin.Show()
Next
Me.Hide()
If con.State = ConnectionState.Open Then
con.Close()
End If
dr.Close()
Else
For j = 0 To 500
MessageBox.Show("Your Login As User", "Welcome User !")
Me.Hide()
MLSUser.Show()
Next
Me.Hide()
dr.Close()
End If
Else
MsgBox("User does not exist!")
txtusername.Text = ""
txtpassword.Text = ""
txtusername.Focus()
End If
Else
MessageBox.Show("You Don't Have Permission To Login System At This Time")
dr.Close()
con.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
txtusername.Text = ""
txtpassword.Text = ""
dr.Close()
End Try
End If
dr.Close()
If con.State = ConnectionState.Open Then
con.Close()
End If
dr.Close()
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I'll explain some codes of above.
dr means datareader. I made dr as mysql data reader.
Normaly according to the above code when entering wrong user name and password, must show "User does not exist" ,but error happened at this point.
This message not show. and show this error message cought by try catch
"Invalid attempt to access a field before calling read()"
except this error username and password entering parts others work well.
So guys help me to this..... :)
Thank You !