473,405 Members | 2,167 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,405 software developers and data experts.

How to reuse sqldatareader?

I have a sqldatareader that I use to read some data. Later I do a dr.close.

In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm trying
to reuse the var with a totally different stored proc.

When the code runs I get this error on the 2nd dr.Read:
System.InvalidOperationException: Invalid attempt to call Read when reader
is closed. at System.Data.SqlClient.SqlDataReader.ReadInternal(B oolean
setTimeout) at System.Data.SqlClient.SqlDataReader.Read() at
customer_usNewAppt.Page_Load(Object sender, EventArgs e) in
F:\work-related\websites\MySite\customer\ucNewAppt.ascx.vb :line 92

Any ideas why? Thanks!
Jun 27 '08 #1
4 3637
After you do your dr.Close() try also calling Dispose and setting it to
null. That should help ensure that the object is definitely not using the
previous one.

In this case though, I don't think you'll gain anything by re-using the same
variable. You'll still end up using separate data connections so you
shouldn't really get any benefit.

If these items are at all similar and you really wanted to re-use resources,
you could see if you can combine them into one stored procedure that can
call the other two stored procedures. This lets you get the first data, then
use the NextResultset method to attempt to load the second set of data. This
re-uses the connection and reduces the roundtrip. The NextResultset will
return a true if there is a next resultset and a false otherwise so it lets
you test to ensure that the data was really returned before you attempt to
read it. This may not be at all what you are trying to do, but since you
were eager to re-use resources I thought I'd mention it. It's a great little
trick especially when attempting to load similar data, such as when you're
trying to populate lists of data for a particular form (such as states,
countries, etc.).

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
"Cirene" <ci****@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP02.phx.gbl...
I have a sqldatareader that I use to read some data. Later I do a
dr.close.

In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm
trying to reuse the var with a totally different stored proc.

When the code runs I get this error on the 2nd dr.Read:
System.InvalidOperationException: Invalid attempt to call Read when reader
is closed. at System.Data.SqlClient.SqlDataReader.ReadInternal(B oolean
setTimeout) at System.Data.SqlClient.SqlDataReader.Read() at
customer_usNewAppt.Page_Load(Object sender, EventArgs e) in
F:\work-related\websites\MySite\customer\ucNewAppt.ascx.vb :line 92

Any ideas why? Thanks!
Jun 27 '08 #2
"Cirene" <ci****@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP02.phx.gbl...
I have a sqldatareader that I use to read some data. Later I do a
dr.close.

In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm
trying to reuse the var with a totally different stored proc.

When the code runs I get this error on the 2nd dr.Read:
System.InvalidOperationException: Invalid attempt to call Read when reader
is closed. at System.Data.SqlClient.SqlDataReader.ReadInternal(B oolean
setTimeout) at System.Data.SqlClient.SqlDataReader.Read() at
customer_usNewAppt.Page_Load(Object sender, EventArgs e) in
F:\work-related\websites\MySite\customer\ucNewAppt.ascx.vb :line 92

Any ideas why? Thanks!
A DataReader is a forward-only read-only set of data. You have only one
opportunity to make use of it. Once you've read to the end, that's it - you
can't go back... This has the effect of making the DataReader object very
light and, generally, speaking, it will out-perform other types of data
sets...

Therefore, if you need anything other than a one-time-only read of the data,
then a DataReader is the wrong ADO.NET object.

Try a DataSet instead...

Alternatively, if the amount of data in the DataReader is (relatively)
small, you could always read it into another object, maybe a generic...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #3
Thanks. I have to look into generics and what that is all about. I have NO
clue. :)

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oi*************@TK2MSFTNGP06.phx.gbl...
"Cirene" <ci****@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP02.phx.gbl...
>I have a sqldatareader that I use to read some data. Later I do a
dr.close.

In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm
trying to reuse the var with a totally different stored proc.

When the code runs I get this error on the 2nd dr.Read:
System.InvalidOperationException: Invalid attempt to call Read when
reader is closed. at
System.Data.SqlClient.SqlDataReader.ReadInternal( Boolean setTimeout) at
System.Data.SqlClient.SqlDataReader.Read() at
customer_usNewAppt.Page_Load(Object sender, EventArgs e) in
F:\work-related\websites\MySite\customer\ucNewAppt.ascx.vb :line 92

Any ideas why? Thanks!

A DataReader is a forward-only read-only set of data. You have only one
opportunity to make use of it. Once you've read to the end, that's it -
you can't go back... This has the effect of making the DataReader object
very light and, generally, speaking, it will out-perform other types of
data sets...

Therefore, if you need anything other than a one-time-only read of the
data, then a DataReader is the wrong ADO.NET object.

Try a DataSet instead...

Alternatively, if the amount of data in the DataReader is (relatively)
small, you could always read it into another object, maybe a generic...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #4
Great ideas. Thanks.

"Mark Fitzpatrick" <ma******@fitzme.comwrote in message
news:OW**************@TK2MSFTNGP04.phx.gbl...
After you do your dr.Close() try also calling Dispose and setting it to
null. That should help ensure that the object is definitely not using the
previous one.

In this case though, I don't think you'll gain anything by re-using the
same variable. You'll still end up using separate data connections so you
shouldn't really get any benefit.

If these items are at all similar and you really wanted to re-use
resources, you could see if you can combine them into one stored procedure
that can call the other two stored procedures. This lets you get the first
data, then use the NextResultset method to attempt to load the second set
of data. This re-uses the connection and reduces the roundtrip. The
NextResultset will return a true if there is a next resultset and a false
otherwise so it lets you test to ensure that the data was really returned
before you attempt to read it. This may not be at all what you are trying
to do, but since you were eager to re-use resources I thought I'd mention
it. It's a great little trick especially when attempting to load similar
data, such as when you're trying to populate lists of data for a
particular form (such as states, countries, etc.).

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - Expression
"Cirene" <ci****@nowhere.comwrote in message
news:eQ**************@TK2MSFTNGP02.phx.gbl...
>I have a sqldatareader that I use to read some data. Later I do a
dr.close.

In the same sub I later to "dr = MyCommand.ExecuteReader" because I'm
trying to reuse the var with a totally different stored proc.

When the code runs I get this error on the 2nd dr.Read:
System.InvalidOperationException: Invalid attempt to call Read when
reader is closed. at
System.Data.SqlClient.SqlDataReader.ReadInternal( Boolean setTimeout) at
System.Data.SqlClient.SqlDataReader.Read() at
customer_usNewAppt.Page_Load(Object sender, EventArgs e) in
F:\work-related\websites\MySite\customer\ucNewAppt.ascx.vb :line 92

Any ideas why? Thanks!

Jun 27 '08 #5

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

Similar topics

3
by: Ricola ! | last post by:
Why do I say: SqlDataReader dr; instead of SqlDataReader dr = new SqlDataReader();
7
by: Franck Diastein | last post by:
Hi, when I call ExportData I have this error: Invalid attempt to Read when reader is closed. Telling me that there's a problem with this line: while(_dataR.Read()){ Code:...
3
by: Neil Guyette | last post by:
Hello, Everyone, I'm trying to find information on how to populate a combo box using a SqlDataReader. I want to be able to set the value of the combo's value property different then the...
1
by: Arvind P Rangan | last post by:
Hi All, How do you get all the values of a sqldatareader if it contains multiple resultset. Using sqldatareader.nextresult and sqldatareader.read e.g. While sqldatareader.read ' If not...
4
by: Craig Buchanan | last post by:
I would like to reuse a SQLDataReader that is populated with information as the datasource for multiple dropdownlists. Unfortunately, the first DDL closes the SDR and my code fails on the second...
3
by: Simon | last post by:
Hi all, I'm hoping that some of you clever chaps could offer me some advice on code reuse. You see, whenever I make applications, I typically only find very limited
4
by: mimi | last post by:
Hi Please help me out, I can't find a way to close a sqldatareader when error occur at statement cmd.ExecuteReader(). I can't close it in catch because it is local in try scope and I can't...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.