?
Solved

testing for null in C# oledb dataset.

Posted on 2006-05-23
9
Medium Priority
?
758 Views
Last Modified: 2008-02-01
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))

0
Comment
Question by:claghorn
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:dstanley9
ID: 16745563
Your syntax is right.  What code is surrounding it?  Could resutls1 be null?  
0
 

Author Comment

by:claghorn
ID: 16745721
yes results1 is null. How do you test for this?
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16745799
if (results1 == null)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:claghorn
ID: 16746006
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.

0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16746129
try

if(results1.HasRows())
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 16746133
Sorry, should be

if(results1.HasRows)
0
 

Author Comment

by:claghorn
ID: 16746285
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;
        }
0
 

Author Comment

by:claghorn
ID: 16746331
sorry, not change the value in the dataset itself just in the variable that I'm returning.
0
 
LVL 25

Accepted Solution

by:
dstanley9 earned 60 total points
ID: 16746383
If you execute a query that returns a null value, HasRows will be true, so that doesn't work, but the following should work:

     while (results1.Read())
       {    
          if (results1.IsDBNull(0)
          {
            newRecNum = 1;
          }
          else
          {
            newRecNum = results1.GetInt32(0) + 1;
           }
       }

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question