Results 1 to 8 of 8

Thread: [Resolved] [2005] Error using datareader with mysql

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Resolved [Resolved] [2005] Error using datareader with mysql

    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:
    1. Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
    2.         conn.ConnectionString = sql
    3.  
    4.         Try
    5.             conn.Open()
    6.             SqlCommand.Connection = conn
    7.             SqlCommand.CommandText = "SELECT * from users where username = ?UserName"
    8.             SqlCommand.Parameters.Add("?UserName", txtUserName.Text)
    9.             Dim reader As MySqlDataReader = SqlCommand.ExecuteReader
    10.             reader.Read()
    11.             username = reader.Item("username").ToString
    12.             password = (DecryptText(reader.Item("password").ToString, "aaaa"))
    13.             If txtPassword.Text = password Then
    14.                 MsgBox(username)
    15.                 CheckInAllowed = reader.Item("CheckInAllowed")
    16.                 LookAtRecord = reader.Item("LookAtRecord")
    17.                 ChangeRecord = reader.Item("ChangeRecord")
    18.                 EditUsers = reader.Item("EditUsers")
    19.                 allowexit = reader.Item("allowexit")
    20.             Else
    21.                 MsgBox("error")
    22.             End If
    23.             conn.Close()
    24.         Catch ex As MySqlException
    25.             MessageBox.Show("Error Connecting to Database: " & ex.Message)
    26.             Debug.Write(ex.Message)
    27.         Finally
    28.             conn.Dispose()
    29.         End Try
    30.     End Sub
    Last edited by moviejunkie; Aug 7th, 2006 at 12:44 AM. Reason: Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width