ExecuteReader returns closed DataReader

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