Click here to Skip to main content
11,811,971 members (70,776 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: SQL-CE C# SQL-Server ADO.NET Forms , +
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!
Posted 23-Oct-12 8:31am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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 )
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
0 KrunalRohit 289
1 Abhinav S 215
2 Palash Mondal_ 210
3 CHill60 180
4 Sergey Alexandrovich Kryukov 130
0 OriginalGriff 3,893
1 KrunalRohit 2,441
2 CPallini 2,240
3 Maciej Los 2,155
4 ppolymorphe 1,725

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | Last Updated 23 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100