473,657 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataReader and ExecuteNonQuery

We have an application that uses a SQLDataReader to cycle through a control
table in the database and process records based upon that data. These
records include things like the directory to poll and where to move the
data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If this
operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open reader
on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed to
using the global connection object that the SQLCommand (above) is using?

2. As I am executing a NonQuery why would that conflict with an open reader?
Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In older
versions of VB we found that opening a connection to the same database was
very resource intensive which is why the original code (prior to porting to
..NET) never closed the database connection. Has that been addressed with the
SQLClient object?

Thanks in advance.
Nov 21 '05 #1
8 5104
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
We have an application that uses a SQLDataReader to cycle through a
control table in the database and process records based upon that data.
These records include things like the directory to poll and where to move
the data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If
this operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open reader
on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed to
using the global connection object that the SQLCommand (above) is using?

2. As I am executing a NonQuery why would that conflict with an open
reader? Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In
older versions of VB we found that opening a connection to the same
database was very resource intensive which is why the original code (prior
to porting to .NET) never closed the database connection. Has that been
addressed with the SQLClient object?

Thanks in advance.

Nov 21 '05 #2
Stephen Costanzo wrote:
We have an application that uses a SQLDataReader to cycle through a control
table in the database and process records based upon that data. These
records include things like the directory to poll and where to move the
data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If this
operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open reader
on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed to
using the global connection object that the SQLCommand (above) is using?

2. As I am executing a NonQuery why would that conflict with an open reader?
Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In older
versions of VB we found that opening a connection to the same database was
very resource intensive which is why the original code (prior to porting to
.NET) never closed the database connection. Has that been addressed with the
SQLClient object?

Thanks in advance.


1. Open Connection -> Execute Reader -> Loop Reader -> Close Reader
You don't need to close the connection, just close the reader. Then you
will be able to do other calls on the connection.

2. The reader is tied to the connection and has the connection locked,
that's where the conflict is.

3. This is by design. A reader doesn't bring all the data down at one
time, only every time do you the reader.read() so the connection must
remain open (and tied up for other calls) to keep the place you are in
your reader.

4. I wouldn't open and close consistantly.

Chris
Nov 21 '05 #3
Thanks for the help.

"Chris" <no@spam.com> wrote in message
news:u1******** ******@TK2MSFTN GP15.phx.gbl...
Stephen Costanzo wrote:
We have an application that uses a SQLDataReader to cycle through a
control table in the database and process records based upon that data.
These records include things like the directory to poll and where to move
the data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If
this operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open
reader on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed
to using the global connection object that the SQLCommand (above) is
using?

2. As I am executing a NonQuery why would that conflict with an open
reader? Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In
older versions of VB we found that opening a connection to the same
database was very resource intensive which is why the original code
(prior to porting to .NET) never closed the database connection. Has that
been addressed with the SQLClient object?

Thanks in advance.


1. Open Connection -> Execute Reader -> Loop Reader -> Close Reader
You don't need to close the connection, just close the reader. Then you
will be able to do other calls on the connection.

2. The reader is tied to the connection and has the connection locked,
that's where the conflict is.

3. This is by design. A reader doesn't bring all the data down at one
time, only every time do you the reader.read() so the connection must
remain open (and tied up for other calls) to keep the place you are in
your reader.

4. I wouldn't open and close consistantly.

Chris

Nov 21 '05 #4
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:u8******** ******@TK2MSFTN GP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
We have an application that uses a SQLDataReader to cycle through a
control table in the database and process records based upon that data.
These records include things like the directory to poll and where to move
the data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If
this operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open
reader on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed
to using the global connection object that the SQLCommand (above) is
using?

2. As I am executing a NonQuery why would that conflict with an open
reader? Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In
older versions of VB we found that opening a connection to the same
database was very resource intensive which is why the original code
(prior to porting to .NET) never closed the database connection. Has that
been addressed with the SQLClient object?

Thanks in advance.


Nov 21 '05 #5
Stephen,

Probably is this the information you are looking for.
Have a look at Mars(datareader s) and connection pooling.

http://msdn2.microsoft.com/en-us/library/ex6y04yf

I hope this helps

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:ef******** ******@TK2MSFTN GP09.phx.gbl...
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:u8******** ******@TK2MSFTN GP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
We have an application that uses a SQLDataReader to cycle through a
control table in the database and process records based upon that data.
These records include things like the directory to poll and where to
move the data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If
this operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open
reader on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed
to using the global connection object that the SQLCommand (above) is
using?

2. As I am executing a NonQuery why would that conflict with an open
reader? Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In
older versions of VB we found that opening a connection to the same
database was very resource intensive which is why the original code
(prior to porting to .NET) never closed the database connection. Has
that been addressed with the SQLClient object?

Thanks in advance.



Nov 21 '05 #6
very cool.

Thanks

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:ek******** ********@TK2MSF TNGP14.phx.gbl. ..
Stephen,

Probably is this the information you are looking for.
Have a look at Mars(datareader s) and connection pooling.

http://msdn2.microsoft.com/en-us/library/ex6y04yf

I hope this helps

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:ef******** ******@TK2MSFTN GP09.phx.gbl...
So in Net 2.x will there be the ability to have more than one open
connection at the same time?

Do you know if there is something on the microsoft site? i tried several
keywords and was not getting meaningful results.

Thanks

"Cor Ligthert [MVP]" <no************ @planet.nl> wrote in message
news:u8******** ******@TK2MSFTN GP12.phx.gbl...
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor

"Stephen Costanzo" <sx********@hot mail.com> schreef in bericht
news:Ov******** ******@TK2MSFTN GP10.phx.gbl...
We have an application that uses a SQLDataReader to cycle through a
control table in the database and process records based upon that data.
These records include things like the directory to poll and where to
move the data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses
the SQLCommand.Exec uteNonQuery function to execute the stored
procedure. If this operation fires against the open connection (we
aren't closing the connection) we get an error indicating that there is
already a open reader on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed
to using the global connection object that the SQLCommand (above) is
using?

2. As I am executing a NonQuery why would that conflict with an open
reader? Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We
noticed this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In
older versions of VB we found that opening a connection to the same
database was very resource intensive which is why the original code
(prior to porting to .NET) never closed the database connection. Has
that been addressed with the SQLClient object?

Thanks in advance.



Nov 21 '05 #7
Cor,

What are you talking about here? You can have as many open connections as
you want.

Perhaps you meant that you can only have one open data reader per connection?

Kerry Moorman
"Cor Ligthert [MVP]" wrote:
Sephen

In Net 1.x is only one open connection at the same time allowed.

I hope this helps,

Cor


Nov 21 '05 #8
Stephen,

Using one open global connection is a design mistake in .Net.

You need to open and close connections each time you access the database.

Kerry Moorman
"Stephen Costanzo" wrote:
We have an application that uses a SQLDataReader to cycle through a control
table in the database and process records based upon that data. These
records include things like the directory to poll and where to move the
data.

During this process there is a timer that fires which reports that the
application is running to the database, sort of a heartbeat. It uses the
SQLCommand.Exec uteNonQuery function to execute the stored procedure. If this
operation fires against the open connection (we aren't closing the
connection) we get an error indicating that there is already a open reader
on the connection object.

My questions are as follows:
1. What are we doing wrong? Should we be doing:
open connection
execute the SQLCommand.Exec uteReader
loop through the reader
close the connection

and have a different connection object for the SQL Statement as opposed to
using the global connection object that the SQLCommand (above) is using?

2. As I am executing a NonQuery why would that conflict with an open reader?
Nothing is being returned relative to a Reader object.

3. Why can't we have multiple SQLDataReaders on a connection? We noticed
this as a problem as well. Is this a bug or "as designed"?

4. Is the best practice to open and close connections consistantly? In older
versions of VB we found that opening a connection to the same database was
very resource intensive which is why the original code (prior to porting to
..NET) never closed the database connection. Has that been addressed with the
SQLClient object?

Thanks in advance.

Nov 21 '05 #9

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

Similar topics

4
3052
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 update another table with that information. However, I'm getting the following error message. There is already an open DataReader associated with this Connection which must be closed first. Description: An unhandled exception occurred during the...
4
3515
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view and bind them to a datagrid using BindingList. I am in need of an epiphany here. Thank you. -Greg ******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID ************************ private Customer c; c =...
3
9464
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code below but I get an exception thrown because the column doesn't exist in Datareader B. I'm not opposed to using another method but the same thing happened when using a Dataset. How do I do this??
20
7201
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 get the error "There is already an open DataReader associated with this Connection which must be closed first." How can I fix this error ? For each DataReader, do I want to open and close the connection (in this case adoCon) to avoid this error ?...
13
3406
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 System.Data.sqlclient
0
2880
by: Teo | last post by:
Hi!! I have been trying to fix an error I keep getting on my VB.NET code. For some reason, I get an error saying "Invalid attempt to access a field before calling Read()" everytime. As you can see in my code below, I created the connection, connection string, etc and I opened the connection, executed the reader and then called the datareader.read() before any variable assignment below. I keep getting that error and I don't know why! I am...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.