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
        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
                Username = True
            End If
        End While
        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

Recommended Answers

All 2 Replies

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

Solved it on my own. anyway thank you again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.