InvalidOperationException: There is already an open DataReader ass

G

Guest

Hi,

I keep getting the following error message:

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

I have read up about it and realise that you can only have one DataReader
open on a Connection at a time, but don't know how to rewrite my code to work
properly.

Currently I have the following:


Protected Shared myDA As SqlDataAdapter

Protected Shared ReadOnly Property DataAdapter() As SqlDataAdapter
Get
If myDA Is Nothing Then
myDA = New SqlDataAdapter("SELECT * FROM Course",
CData.Connection)
Dim cb As New SqlCommandBuilder(myDA)
myDA.UpdateCommand = cb.GetUpdateCommand()
myDA.InsertCommand = cb.GetInsertCommand()
myDA.DeleteCommand = cb.GetDeleteCommand()
CData.Connection.Close()
End If
Return myDA
End Get
End Property


Public Shared Function GetCourse(ByVal CourseID As Integer) As CCourse
Dim selectText As String = DataAdapter.SelectCommand.CommandText
DataAdapter.SelectCommand.CommandText = "SELECT * FROM Course WHERE
CourseID =" & CourseID
Dim dt As New DataTable
DataAdapter.Fill(dt)
DataAdapter.SelectCommand.CommandText = selectText
If dt.Rows.Count = 0 Then
Return Nothing
Else
Return New CCourse(dt.Rows(0))
End If
End Function


When I try to access the GetCourse Function from two different computers at
the same time it gives the error as mentioned above.

Can anyone help me to solve this problem... i.e. am I writing my code
completely wrong.

Any help would be much appreciated.

Thanks in advance.

Grant.
 
G

Guest

Sorry, forgot to put that I am using .Net 1.1 with service pack 2, and
connecting to SQL Server 2005.

Thanks.
 
J

Joanna

Helo we have the same problem wth our application. We use .net 1.1 and sql 2005. Code works fine with one user working on it but if two or more users start to access the code with DataReader from two different computers at the same time it gives the error "There is already an open DataReader associated
with this Connection which must be closed first.". We are 1005 sure we close our reader and connection properly.

Have you managed to resolve this problem since your post?
Joanna

From http://www.developmentnow.com/g/8_2...on-There-is-already-an-open-DataReader-ass.ht

Posted via DevelopmentNow.com Group
http://www.developmentnow.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top