Link to home
Start Free TrialLog in
Avatar of claghorn
claghorn

asked on

testing for null in C# oledb dataset.

I can't seem to figure out how to test for a null in a oledb dataset. I am pulling one value from a table. I am trying to use the line below but it is an error to refer to something that is null. I've tried several combinations with isdbnull but no success and can't this info in any book. What is the way to use isdbnull without getting an error?

if (results1.IsDBNull(0))

Avatar of dstanley9
dstanley9

Your syntax is right.  What code is surrounding it?  Could resutls1 be null?  
Avatar of claghorn

ASKER

yes results1 is null. How do you test for this?
if (results1 == null)
I tried:
if (results1 == null)

no, it does not work. I think the resultset is returning a row though, only the value I'm specifying in my query is emtpy. How do you test for this?
My query is:
select max("rec_num") as max from "phonehistory" where "id"= 'drf321'
The resultset when run in postgres returns a row with rec_num empty.

try

if(results1.HasRows())
Sorry, should be

if(results1.HasRows)
I'm posting the whole section because this is not working.
How do you negate the "hasrows" line?
Also, if "hasrows" is true and the value in the row is 1 then why does my attempt to add 1 to it (to make it 2) fail? see below.
All I want to do is set recnum to 1 if it does not exist and if it does exist then add 1 to it.

if (results1.HasRows) == false
        //if (results1 == null)
        {
            //while (results1.Read())
            //{
            //      newRecNum = results1.GetInt32(1 - 1) + 1;
            //}
            newRecNum = 1;
        }
        else
        //if (newRecNum == 0)
        //{
        //      newRecNum = 1;
        //}
        {
            newRecNum = results1.GetInt32(1 - 1) + 1;
        }
sorry, not change the value in the dataset itself just in the variable that I'm returning.
ASKER CERTIFIED SOLUTION
Avatar of dstanley9
dstanley9

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial