I am making a program which requires the user to login in, it is a simple form with a username and password text boxes and a login buttion, all the user information is stored on a MySqlDatabase. I can connect and read from the database, the trouble I am having is when the user types a username that is not in the database it give me an error of "Invalid attempt to access a field before calling Read()"
HELP. Here is my code for the login button
VB Code:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click conn.ConnectionString = sql Try conn.Open() SqlCommand.Connection = conn SqlCommand.CommandText = "SELECT * from users where username = ?UserName" SqlCommand.Parameters.Add("?UserName", txtUserName.Text) Dim reader As MySqlDataReader = SqlCommand.ExecuteReader reader.Read() username = reader.Item("username").ToString password = (DecryptText(reader.Item("password").ToString, "aaaa")) If txtPassword.Text = password Then MsgBox(username) CheckInAllowed = reader.Item("CheckInAllowed") LookAtRecord = reader.Item("LookAtRecord") ChangeRecord = reader.Item("ChangeRecord") EditUsers = reader.Item("EditUsers") allowexit = reader.Item("allowexit") Else MsgBox("error") End If conn.Close() Catch ex As MySqlException MessageBox.Show("Error Connecting to Database: " & ex.Message) Debug.Write(ex.Message) Finally conn.Dispose() End Try End Sub




Reply With Quote