Two SqlDataReader for the same connection

R

Ruslan

Hello,



I have:





Method1()

{

SqlDataReader dr = new DataReader();

....

while (dr.Read)

{

Method2()

}

}



Method2()

{

SqlDataReader dr = new DataReader();

....

while (dr.Read)

{

Method2()

}

}



I use the same connection for both SqlDataReader. It's logically what error
appears, when the "dr.Read()" from the "Method2 is called, because there is
one open connection for the first SqldDataReader. Is it possible to do
something what the both SqlDataReader use the same connection?



Thanks,



Ruslan
 
M

Marina

It is not possible to have 2 open data readers on the same connection at the
same time.

Either use 2 connections, or use a datatable/dataset for the result of one
or both of your queries.
 
W

William Ryan

Close the first datareader or use the CommandBehavior.CloseConnection
www.knowdotnet.com/articles/schemas2.html
.. If you close the reader, you won't have to reopen the connection which
may or may not be appropriate depending on your particular needs. ANother
thing...you may want to use a new connection and just reuse the
connectionstring and throw in a try/catch/finally to make sure things get
cleaned up if anything goes wrong.

HTH,

Bill
 
R

Ruslan

Thank you, but I need to use the same connection and not to close the first
reader, because when I go back to Method1 from Method2 it needs the reader
(dr.Read()).

I think it is not possible and I need to use DataAdapter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top