469,932 Members | 2,758 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,932 developers. It's quick & easy.

InvalidOperationException: There is already an open DataReader ass

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.
Nov 19 '05 #1
2 2663
Sorry, forgot to put that I am using .Net 1.1 with service pack 2, and
connecting to SQL Server 2005.

Thanks.

"Grant" wrote:
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.

Nov 19 '05 #2
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_20...Reader-ass.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Sep 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by rn5a | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.