Two SqlDataReader for the same connection

  • Thread starter Thread starter Ruslan
  • Start date Start date
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
 
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.
 
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
 
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.
 
Back
Top