Sql objConnection woes

G

Guest

I have a class method that returns a DataReader

SqlDataReader parentReader = class.GetResults("55");
if(parentReader.Read())
{
..
..
}


public SqlDataReader GetResults(string County)
{
SqlDataReader myreader= My SQL statements
..
..
..
objConnection.Dispose(); //Problem is here
return myReader
}

The prob is if the parant code attempts to read from myReader, it says it
cant b/c the datareader is closed. Yet at this point the fact that the
connection created in the child class was disposed shouldnt affect a
datareader thats already loaded. The connection was local to the child class,
why should the parent datareader care? Whats the problem? If I remove the
dispose method, everything works, but I run the risk of running out of
connections in the SQL pool b/c the code is called many times.
 
K

Kevin Spencer

A DataReader maintains a constant connection to the DataStore, as it only
fetches one row at a time. Therefore, as long as you are using the
DataReader, the Connection must remain opened.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
J

John Bailo

JP said:
The prob is if the parant code attempts to read from myReader, it says it
cant b/c the datareader is closed. Yet at this point the fact that the
connection created in the child class was disposed shouldnt affect a
datareader thats already loaded. The connection was local to the child
class, why should the parent datareader care? Whats the problem? If I
remove the dispose method, everything works, but I run the risk of running
out of connections in the SQL pool b/c the code is called many times.

You should serialize the data either to XML ( ExecuteXML ) or store the
contents to a DataSet.
 

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

Top