I am using a sqldatareader with this command:

select max(id) from x

If there is no rows in table x then using
sqldatareader.read() still return true as if
there is a row, which means this code breaks:

if (sqldatareader.read()) {
... = sqldatareader.GetInt32(0); // field 0 is null!?
}

All the examples i have seen on ms website and
in books dont take any precautions for empty
tables. Right now i am doing this:

if (sqldatareader.read() && !sqldatareader.IsDBNull(0)) {
... = sqldatareader.GetInt32(0);
}

It seems clumsy. I am doing somthing wrong here?

Re: SqlDataReader with no rows. by Jon

Jon
Fri Feb 20 03:42:31 CST 2004

me <wtumaxi@forum.dk> wrote:
> I am using a sqldatareader with this command:
>
> select max(id) from x
>
> If there is no rows in table x then using
> sqldatareader.read() still return true as if
> there is a row, which means this code breaks:
>
> if (sqldatareader.read()) {
> ... = sqldatareader.GetInt32(0); // field 0 is null!?
> }
>
> All the examples i have seen on ms website and
> in books dont take any precautions for empty
> tables. Right now i am doing this:
>
> if (sqldatareader.read() && !sqldatareader.IsDBNull(0)) {
> ... = sqldatareader.GetInt32(0);
> }
>
> It seems clumsy. I am doing somthing wrong here?

Nope - the result of your select is *exactly* a table with a single
row, which has a null value. If you execute the query in SQL Server
Enterprise Manager you'll see the same behaviour.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Re: SqlDataReader with no rows. by Sheila

Sheila
Fri Feb 20 08:01:10 CST 2004

Hello,

If you're using version 1.1 of the .Net framework, you can use the HasRows
property to test for this.

Hope that helps!


"me" <wtumaxi@forum.dk> wrote in message
news:6fd6d897.0402200133.6b4cbf49@posting.google.com...
> I am using a sqldatareader with this command:
>
> select max(id) from x
>
> If there is no rows in table x then using
> sqldatareader.read() still return true as if
> there is a row, which means this code breaks:
>
> if (sqldatareader.read()) {
> ... = sqldatareader.GetInt32(0); // field 0 is null!?
> }
>
> All the examples i have seen on ms website and
> in books dont take any precautions for empty
> tables. Right now i am doing this:
>
> if (sqldatareader.read() && !sqldatareader.IsDBNull(0)) {
> ... = sqldatareader.GetInt32(0);
> }
>
> It seems clumsy. I am doing somthing wrong here?