User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 374,173 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,445 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 932 | Replies: 15
Reply
Join Date: Sep 2007
Posts: 77
Reputation: justapimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
justapimp justapimp is offline Offline
Junior Poster in Training

Help How do I Evaluate Null value from database?

  #1  
Mar 13th, 2008
I have been looking for a way to evaluate if a column in a database has a null value in it. So far, every method suggested have failed and I would like to know anyone else have successfully evaluate a null value in C# Visual Studio 2005.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2008
Posts: 42
Reputation: bbqchickenrobot is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
bbqchickenrobot bbqchickenrobot is offline Offline
Light Poster

Re: How do I Evaluate Null value from database?

  #2  
Mar 13th, 2008
you can use nullable types i.e. - int? myVar = ... ;

you can also check to see if the value is = to System.DbNull...
Reply With Quote  
Join Date: Sep 2007
Posts: 77
Reputation: justapimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
justapimp justapimp is offline Offline
Junior Poster in Training

Re: How do I Evaluate Null value from database?

  #3  
Mar 14th, 2008
How exactly do I do that, I've use IsDBNull, Convert.IsDBNull, IsDBNull.Value and nothing works and I've tried type casting. ????
Reply With Quote  
Join Date: Mar 2008
Posts: 20
Reputation: ravichandra is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 4
ravichandra ravichandra is offline Offline
Newbie Poster

Help Re: How do I Evaluate Null value from database?

  #4  
Mar 14th, 2008
The following can help you but i am sure whether this is the right way.

While selecting the column check for null values like

select ISNULL(class,-1) from student

If the column class contains null values,the query returns -1 value.
Using this value you can identity the column contains null value.

Good Luck.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,161
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How do I Evaluate Null value from database?

  #5  
Mar 14th, 2008
You need to compare the column value with System.DBNull.Value

So assuming a DataTable for example:

if( myDataTable.Rows[0]["someColumn"].Equals(System.DBNull.Value) )
{
          //Handle situation for null value.
}
Last edited by hollystyles : Mar 14th, 2008 at 4:59 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Sep 2007
Posts: 77
Reputation: justapimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
justapimp justapimp is offline Offline
Junior Poster in Training

Re: How do I Evaluate Null value from database?

  #6  
Mar 14th, 2008
I tried that method already and it does not work.
Reply With Quote  
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,161
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Rep Power: 7
Solved Threads: 58
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: How do I Evaluate Null value from database?

  #7  
Mar 14th, 2008
Hmm... well it works for me. You'll have to post some code or give more information.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote  
Join Date: Dec 2007
Posts: 286
Reputation: ericstenson is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 28
Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

Re: How do I Evaluate Null value from database?

  #8  
Mar 14th, 2008
you can also get the value of the cell and use .text.tostring
then test the length of the string len(abc) > 0
--
"Dummy."
Reply With Quote  
Join Date: Sep 2007
Posts: 77
Reputation: justapimp is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
justapimp justapimp is offline Offline
Junior Poster in Training

Re: How do I Evaluate Null value from database?

  #9  
Mar 14th, 2008
Here is the code

[WebMethod]
    public int TestDBNullValue(string PID)
    {
        int ErrorValue = 0;
        if (PID.Equals(String.Empty))
        {
            ErrorValue = 10110; //PID is null or empty
        }
        else
        {
            DeviceTableAdapter DeviceAdapter = new DeviceTableAdapter();
            JeanPierre.DeviceDataTable DeviceTable = DeviceAdapter.GetDeviceUIDByPID(PID);

            if (DeviceTable.Count.Equals(0))
            {
                ErrorValue = 1111; //No device found
            }
            else
            {
                JeanPierre.DeviceRow DevRows = DeviceTable[0];

                if(DevRows.TrackingMode.Equals(System.DBNull.Value))
                {
                    ErrorValue = 1;
                }
                else
                {
                    ErrorValue = 41;
                }
            }
        }
        return (ErrorValue);
    }
Reply With Quote  
Join Date: Dec 2007
Posts: 286
Reputation: ericstenson is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 28
Colleague
ericstenson's Avatar
ericstenson ericstenson is offline Offline
Posting Whiz in Training

Re: How do I Evaluate Null value from database?

  #10  
Mar 14th, 2008
i converted from vb.net to c#, so it might not be accurate...

{
if (Strings.len(PID) == 0) {
}
}
--
"Dummy."
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 4:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC