Results 1 to 8 of 8

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

  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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Error using datareader with mysql

    If the user enters an invalid user name then the reader will not contain any rows, so you can't read any rows. If the reader contains no rows then Read will return False, so when you call Read you have to test its return value.

    Also, you're going about your password validation all wrong. You shouldn't be getting the record that corresponds to a user name and then decrypting the password it contains and then comparing that to the password provided. You should be encrypting the password provided and then querying the database for a record that has the provided user name AND that password. Normally you would use a one-way encryption scheme too, so that there is no way someone can get hold of your encryption algorithm and use it to decrypt all the passwords in the database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Re: [2005] Error using datareader with mysql

    Ok I am still having problems... Help..... ok now that is out of my sysytem here is my updated code:

    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.         Sentpassword = (EncryptText(txtPassword.Text, "aaaa"))
    4.         Try
    5.             conn.Open()
    6.             SqlCommand.Connection = conn
    7.             SqlCommand.CommandText = "SELECT * from users where username = ?UserName AND password = ?Password"
    8.             SqlCommand.Parameters.Add("?UserName", txtUserName.Text)
    9.             SqlCommand.Parameters.Add("?Password", Sentpassword)
    10.             Dim reader As MySqlDataReader = SqlCommand.ExecuteReader
    11.             If reader.Read = True Then
    12.                 username = reader.Item("username").ToString
    13.                 MsgBox(username)
    14.                 CheckInAllowed = reader.Item("CheckInAllowed")
    15.                 LookAtRecord = reader.Item("LookAtRecord")
    16.                 ChangeRecord = reader.Item("ChangeRecord")
    17.                 EditUsers = reader.Item("EditUsers")
    18.                 allowexit = reader.Item("allowexit")
    19.             Else
    20.                 MsgBox("error")
    21.             End If
    22.             conn.Close()
    23.         Catch ex As MySqlException
    24.             MessageBox.Show("Error Connecting to Database: " & ex.Message)
    25.             Debug.Write(ex.Message)
    26.         Finally
    27.             conn.Dispose()
    28.         End Try
    29.     End Sub

    I am still perplexed on how I can use the ".reader" to tell if the user can login in or not.

    Oh and thanks for helping. I love this site

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Error using datareader with mysql

    Quote Originally Posted by moviejunkie
    I am still perplexed on how I can use the ".reader" to tell if the user can login in or not.
    If Read returns False that does NOT mean an error has occurred. As I said before, it means that your query has not matched any rows, which means there is no user with those credentials. If Read returns True then that means you have matched a row, so there is a user with those credentials so the login is successful.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

    Resolved Re: [2005] Error using datareader with mysql

    Thank you for your help I got it to work....well kind of....if the user does not have a password it does not work if you leave the pass text box blank. Oh well I guess I will have to have the people that do not want a password not be able to log in. (I think it has something with the encryption)

    Well here is my working code:

    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.         Try
    4.             conn.Open()
    5.             SqlCommand.Connection = conn
    6.             SqlCommand.CommandText = "SELECT * from users where username = ?UserName AND password = ?Password"
    7.             SqlCommand.Parameters.Add("?UserName", txtUserName.Text)
    8.             SqlCommand.Parameters.Add("?Password", (EncryptText(txtPassword.Text, "aaaa")))
    9.             Dim reader As MySqlDataReader = SqlCommand.ExecuteReader
    10.             If reader.Read Then
    11.                 username = reader.Item("username").ToString
    12.                 MsgBox(username)
    13.                 CheckInAllowed = reader.Item("CheckInAllowed")
    14.                 LookAtRecord = reader.Item("LookAtRecord")
    15.                 ChangeRecord = reader.Item("ChangeRecord")
    16.                 EditUsers = reader.Item("EditUsers")
    17.                 allowexit = reader.Item("allowexit")
    18.             Else
    19.                 MsgBox("Either your user name or password was wrong, try again", MsgBoxStyle.OkOnly, "Error")
    20.             End If
    21.             conn.Close()
    22.         Catch ex As MySqlException
    23.             MessageBox.Show("Error Connecting to Database: " & ex.Message)
    24.             Debug.Write(ex.Message)
    25.         Finally
    26.             conn.Dispose()
    27.         End Try
    28.     End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Why don't you rewrite EncryptText to simply return an empty string if it's passed an empty string, and the same for DecryptText?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    19

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

    could do that....man your smart thanks for all the help, I am new at this db programming stuff...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

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

    Don't mistake experience for intelligence.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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