Home All Groups Group Topic Archive Search About

ExecuteReader returns closed DataReader

Author
26 Apr 2006 6:04 PM
PIEBALD
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?)

Author
26 Apr 2006 6:57 PM
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.

Show quote
"PIEBALD" <PIEB***@discussions.microsoft.com> wrote in message
news:942AB81E-4D97-4597-BB8B-5DC95EA0B731@microsoft.com...
> 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?)
Author
26 Apr 2006 9:59 PM
PIEBALD
> 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.

AddThis Social Bookmark Button