0

Hey guys im having problem with the registration form that i created the error shows after i tried some error handling if the username already exists

this is the error message:

{"Invalid attempt to Read when reader is closed."}

and this is the error highlight

While mydr.Read()

Heres my code for the register button

Private Sub cmdReg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReg.Click
        conn = New MySqlConnection("host=localhost; uid=root; pwd=root; database=mydatabase")

        Dim Username As Boolean = True
        conn.Open()
        Dim mystr As String = "SELECT * FROM tbluser WHERE Username = '" & txtUser.Text & "';"
        Dim mydr As MySqlDataReader
        Dim myda As New MySqlDataAdapter
        Dim mycmd As New MySqlCommand
        mycmd.CommandText = mystr
        mycmd.Connection = conn
        myda.SelectCommand = mycmd
        mydr = mycmd.ExecuteReader
        While mydr.Read()
            If mydr.HasRows = True Then
                MsgBox("Username Already exsist! Please choose another username.")
                Username = False
                mydr.Close()
            Else
                mydr.Close()
                Username = True
            End If
        End While
        mydr.Close()
        If Username = True Then
            Dim mystr2 As String = "INSERT INTO tbluser (FirstName, LastName, ContactNum, Username, Password)VALUES ('" & txtUF.Text & "','" & txtUL.Text & "','" & txtUC.Text & "','" & txtUser.Text & "','" & txtPass.Text & "')"
            Dim mydr2 As MySqlDataReader
            Dim myda2 As New MySqlDataAdapter
            Dim mycmd2 As New MySqlCommand
            mycmd2.CommandText = mystr2
            mycmd2.Connection = conn
            myda2.SelectCommand = mycmd2
            mydr2 = mycmd2.ExecuteReader
            MsgBox("You've Registered!")
        End If
    End Sub
2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by kenomote
0

You close the reader in the while loop (and after). Why don't you change the while to an if, makes much more sense.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.