Click here to Skip to main content
14,699,886 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi guys,

I am running into issues with checking for empty datareaders. I am using Visual Studio 2012 with SQL Server Compact 4.0. The HasRows property is not supported in Compact, so I can't use that.

This is what I have right now.

if ( ( (ac_key_number_dr.Read()) && (cc_key_number_dr.Read()) && (key_number_dr.Read()) && (hybrid_key_number_dr.Read()) && (transmission_key_number_dr.Read()) ) == false )

I am checking for all 5 of my datareaders as to whether or not they are empty.

My intended target is that if all 5 are empty to call this function "loadDefault()". Otherwise, if one of the readers has content, this if statement will be skipped.

I'm not getting any error message, but the output right now is that this if statement returns true and this function gets called no matter if all are empty or if one of the readers has content. What am I missing?

Thanks in advance!

Assuming Read() returns TRUE if Read() succeeds then you want change your logic as below.
If any Read() succeeds loadDefault() will not get called.

if ( ( (ac_key_number_dr.Read()) || (cc_key_number_dr.Read()) || (key_number_dr.Read()) || (hybrid_key_number_dr.Read()) || (transmission_key_number_dr.Read()) ) == false )
Your logic has a small flaw:
Read returns true if it has data, so
a.Read() && b.Read()

is true if and only if a and b both have data.
(a.Read() && b.Read()) == false

if true if either of a or b does not contain data, or if both do not
If you want your statement executed only when neither have data, then you want
!a.Read() && !b.Read()
Which is true only when they are both empty.

So you want to code it as:
if ( !ac_key_number_dr.Read() && !cc_key_number_dr.Read() && !key_number_dr.Read() && !hybrid_key_number_dr.Read() && !transmission_key_number_dr.Read())

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900