473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2841
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason | last post by:
Hi, {asp .net 1.1, sp 2) I am facing problems with multi users accessing the same page. There is a datagrid in the webform. The "datareader already opened error/ object reference not found"...
2
by: rn5a | last post by:
The following code resides in a VB class file name GetOrder.vb (this class file exists in the App_Code directory): Namespace Shop Public Class Orders Public Function ViewOrder(ByVal UserID As...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
10
by: jimmy | last post by:
Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
13
by: Bart | last post by:
Hi, i get the error: "There is already an open DataReader associated with this Command which must be closed first" Thanks Bart ----------------------------------------- Imports...
3
by: BLUE | last post by:
I've a TransactionScope in which I select some data and I want to do some queries for each record retrieved with my select. I'm using a DataReader and for each record I do factory.CreateCommand()...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.