473,396 Members | 1,832 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,396 software developers and data experts.

DataReader Already Open

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 Integer, ByVal
OrderID As Integer) As SqlDataAdapter
Dim sqlDapter As SqlDataAdapter
Dim sqlConn As SqlConnection

sqlConn = New SqlConnection("......")

sqlDapter = New SqlDataAdapter
sqlDapter.SelectCommand = New SqlCommand("spViewOrder",
sqlConn)
sqlDapter.SelectCommand.CommandType =
CommandType.StoredProcedure

Try
With sqlDapter.SelectCommand
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
.Parameters.Add("@OrderID", SqlDbType.Int).Value =
OrderID
End With

sqlConn.Open()
If (sqlDapter.SelectCommand.ExecuteReader.HasRows) Then
Return sqlDapter
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace

This is how an ASPX page named GetOrder.aspx accesses the above
function which returns a SqlDataAdapter:

<%@ Import Namespace="Shop" %>
'import other namespaces

Sub UserCart()
Dim dSet As DataSet
Dim boOrders As Orders
Dim sqlDapter As SqlDataAdapter

dSet = New DataSet
boOrders = New Orders

sqlDapter = boOrders.ViewOrder(iUserID, iOrderID)
sqlDapter.Fill(dSet)
End Sub

When I run the ASPX page, the following error gets generated:

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

pointing to the sqlDapter.Fill(dSet) line.

How do I overcome this error?

Nov 24 '06 #1
2 3162
Consume the data it is already getting or close the reader or use a
different adapter. The pattern used passes back is in the midst of a query
(how many rows) that is pulled by a reader.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com

********************************************
Think outside the box!
********************************************
<rn**@rediffmail.comwrote in message
news:11**********************@45g2000cws.googlegro ups.com...
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 Integer, ByVal
OrderID As Integer) As SqlDataAdapter
Dim sqlDapter As SqlDataAdapter
Dim sqlConn As SqlConnection

sqlConn = New SqlConnection("......")

sqlDapter = New SqlDataAdapter
sqlDapter.SelectCommand = New SqlCommand("spViewOrder",
sqlConn)
sqlDapter.SelectCommand.CommandType =
CommandType.StoredProcedure

Try
With sqlDapter.SelectCommand
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
.Parameters.Add("@OrderID", SqlDbType.Int).Value =
OrderID
End With

sqlConn.Open()
If (sqlDapter.SelectCommand.ExecuteReader.HasRows) Then
Return sqlDapter
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace

This is how an ASPX page named GetOrder.aspx accesses the above
function which returns a SqlDataAdapter:

<%@ Import Namespace="Shop" %>
'import other namespaces

Sub UserCart()
Dim dSet As DataSet
Dim boOrders As Orders
Dim sqlDapter As SqlDataAdapter

dSet = New DataSet
boOrders = New Orders

sqlDapter = boOrders.ViewOrder(iUserID, iOrderID)
sqlDapter.Fill(dSet)
End Sub

When I run the ASPX page, the following error gets generated:

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

pointing to the sqlDapter.Fill(dSet) line.

How do I overcome this error?
Nov 24 '06 #2
This line:

If (sqlDapter.SelectCommand.ExecuteReader.HasRows) Then

causes the DataReader to be created and opened.

What you should do is change the function to fill a dataset and return that,
rather than returning a DataAdapter. The DataSet can then be checked to see
if it contains any data or not. A DataReader is not needed here since you
have a DataAdapter which will do the work.

<rn**@rediffmail.comwrote in message
news:11**********************@45g2000cws.googlegro ups.com...
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 Integer, ByVal
OrderID As Integer) As SqlDataAdapter
Dim sqlDapter As SqlDataAdapter
Dim sqlConn As SqlConnection

sqlConn = New SqlConnection("......")

sqlDapter = New SqlDataAdapter
sqlDapter.SelectCommand = New SqlCommand("spViewOrder",
sqlConn)
sqlDapter.SelectCommand.CommandType =
CommandType.StoredProcedure

Try
With sqlDapter.SelectCommand
.Parameters.Add("@UserID", SqlDbType.Int).Value =
UserID
.Parameters.Add("@OrderID", SqlDbType.Int).Value =
OrderID
End With

sqlConn.Open()
If (sqlDapter.SelectCommand.ExecuteReader.HasRows) Then
Return sqlDapter
Else
Return Nothing
End If
Catch ex As Exception
Throw ex
End Try
End Function
End Class
End Namespace

This is how an ASPX page named GetOrder.aspx accesses the above
function which returns a SqlDataAdapter:

<%@ Import Namespace="Shop" %>
'import other namespaces

Sub UserCart()
Dim dSet As DataSet
Dim boOrders As Orders
Dim sqlDapter As SqlDataAdapter

dSet = New DataSet
boOrders = New Orders

sqlDapter = boOrders.ViewOrder(iUserID, iOrderID)
sqlDapter.Fill(dSet)
End Sub

When I run the ASPX page, the following error gets generated:

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

pointing to the sqlDapter.Fill(dSet) line.

How do I overcome this error?

Nov 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
4
by: Maziar Aflatoun | last post by:
Hi everyone, I am working on the 'Delete' section of my program. What I need to do is query my database and for every ID that it finds I want to remove a file+ID.jpg from my file folder and...
2
by: Grant | last post by:
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...
3
by: Rykie | last post by:
I have an application that constantly needs to read and write data to a SQL box. For most of my read transactions I use a datareader. I compiled a class that has all my datareader commands in it...
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"...
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...
3
by: Johnny Jörgensen | last post by:
I've got an error that I simply cannot locate: I've got a form in which I use a datareader object to read information from a db. After the read, I close the reader and I dispose of both the...
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: 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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.