ExecuteReader returns closed DataReader

G

Guest

Using .net 2 and Sql Server 2005, I am intermittently getting the exception:

Invalid attempt to FieldCount when reader is closed.

What I don't understand is how the DataReader could be closed when it was
just created by ExecuteReader.

As in:

dr = cmd.ExecuteReader() ;
Console.Write ( dr.FieldCount ) ;

If there's a problem (like the connection is closed) then why doesn't
ExecuteReader throw an exception? (For that matter, why doesn't
ExecuteScalar?)
 
M

Marina Levit [MVP]

Even though I haven't tried to confirm this, I am guessing you have to at
least advance to the first row in the result set before trying to look at
the field count,etc.
 
G

Guest

Even though I haven't tried to confirm this, I am guessing you have to at
least advance to the first row in the result set before trying to look at
the field count,etc.

Nope, that seems to work OK, at least most of the time, this is an
intermittent problem. I'll investigate more.
 

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