How to test for open datareader

tcl4p

Well-known member
Joined
Feb 29, 2008
Messages
48
Programming Experience
1-3
Can anyone tell me how to test for an open datareader? I'm getting the following error: "There is already an open datareader associated with this command."
The code I'm using is below. I have no problem calling he procedure the first time through, but when it gets call after being called a second time. I ran it through debug and I can see that the datareader is closed, not to mention the variable reader is declared local, so it should be disposed of at the end of the procedure.
Any help would be appreciated.

Tom

VB.NET:
Private Sub LoadIncidentTypesSelected()
        Dim reader As SqlDataReader
        Dim dt As New DataTable

        Try
            If Not IsNothing(Me.lbIncidentSelected.DataSource) Then
                Me.lbIncidentSelected.DataSource = Nothing
            End If
            Me.lbIncidentSelected.Items.Clear()
            reader = clsIncident.GetIncidentTypesSelected
            Dim tb As New DataTable
            tb.Load(reader)
            Me.lbIncidentSelected.DisplayMember = "IncidentType"
            Me.lbIncidentSelected.ValueMember = "itID"
            Me.lbIncidentSelected.DataSource = tb
            reader.Close()
            Me.lbIncidentSelected.SelectedIndex = -1
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
 
what line causes this? Howcome youre doing your data access in this very weird way? See DW2 link in my signature, section Creating a Simple Data App (and then otehr relevant sections) for the MS recommended way
 
thanks

Thanks for the reply. I never found out how to test for the open datareader, but what I found is the error was coming not from the datareader, but from the DB connection object.
The called code I listed does nothing more then clear and reload a list box and was part of the Load routine for the form. As part of the form processing I call another form(form2) to update or add values to the database. Some of these values are reflected in the list box so I wanted to refresh the listbox when I got back to the original form(form1). I should mention I did not close the calling form(form1), but set up a event listener, which was executed upon closing form2. This worked fine the first time I returned to form1, but any subsequent time I went out and back I got the error message about the open datareader. After looking on the web for answers the only thing I could determine was I was trying to open multiple datareaders on the same connection. I couldn't figure this out since I close the datareader after each use, so what I did was to close the DB connection when I closed the form. This worked, but I'm still not sure exactly why. I assume it has something to do with leaving form1 open and the fact I was calling the event on form1 from form2.
At any rate thanks for trying to help.
 

Latest posts

Back
Top