Results 1 to 4 of 4

Thread: [RESOLVED] DataReader error...SQL connection problem

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Resolved [RESOLVED] DataReader error...SQL connection problem

    Hi! I'm having trouble with my connection between VB and MySQL. I've made a program that displays data in a list box that it gets from my database. I have a Button that when clicked, displays the data in a listbox. When I try to delete part of the data, this error shows up:

    "There is already an open DataReader associated with this Connection which must be closed first."

    Here is the code for the delete button:
    Code:
    Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Delete.Click
            Dim delQuery As New MySqlCommand
            Dim index As Integer
    
            index = DataBox.SelectedIndex
            DataBox.Items.RemoveAt(index)
    
            delQuery.Connection = localConnection
            delQuery.CommandText = "DELETE FROM `temp_data`.`data` WHERE `idx` = '" + idx(index) + "'"
            delQuery.ExecuteScalar()
    
        End Sub
    My display button also follows a similar format in using the SQL connection:
    Code:
            disQuery.Connection = localConnection
            disQuery.CommandText = "SELECT * FROM data"
            disQuery.ExecuteScalar()
    I've tried to insert this after the ExecuteScalar command:
    Code:
    localConnection.Close()
    localconnection.Open()
    This works but I'm afraid it might cause my other functions to not work properly. I have another running thread that constantly sends data to the database every x minutes. I'm afraid that when I click the display or delete button, the program may fail.
    Any suggestions? Thank You!

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: DataReader error...SQL connection problem

    There can only be one datareader open on a connection at a time. If you need a second open a new connection for that datareader and close it (the connection) after you get the data.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: DataReader error...SQL connection problem

    Does that mean I need to make something like a "localconnection2"?

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2011
    Posts
    9

    Re: DataReader error...SQL connection problem

    I got it working! I just made a new MySqlConnection variable. Thanks! Haha.

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