Click here to Skip to main content
Rate this: bad
good
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 )
            {
                loadDefault();
            }
 
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 9:31am
Rate this: bad
good
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 )
            {
                loadDefault();
            }
  Permalink  
Rate this: bad
good
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.
Conversely,
(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())
            {
                loadDefault();
            }
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 23 Oct 2012
Copyright © CodeProject, 1999-2014
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