If you're an old ADO programmer, you may be frustrated by not knowing exactly how to check for NULL. Here's a 1 second tip on how to do it in ADO.NET.
Terms of Agreement:
By using this article, you agree to the following terms...
You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
If you try the old fashioned way of checking for null:
If IsNull(rsCompany("Name")) Then
Visual Studio will tell you that IsNull is not supported and to use DBNull
instead. At first I thought this was really nice. However, when I
tried to do:
If rscompany("Name") = DBNull Then
I got an error about assignments not allowed to a type! What was I to
do?
The Visual Studio help was its usual cryptic self ("The DBNull class is for
handling NULL values"...accurate by not alot of help). After some fumbling
around, I stumbled on the answer, and thought I'd share it to hopefully save
someone 5 minutes of having to research it:
Hi, you can use it this way: var=IIF(IsDBNull(YourValue)=False,TrueCondition,FalseCondition) :-) (If this comment was disrespectful, please report it.)
Good point Igor...I only found out about IsDBNull myself a few days ago (the .NET help is just not that much "help") and meant to post it up...that's a great addendum.
Ian (If this comment was disrespectful, please report it.)
You saved me lots of time and hassle. None of the books I'm using even have a reference to 'null' or 'dbnull'. The little things always seem to cause the biggest problems. Thanks again. (If this comment was disrespectful, please report it.)
Imports System.Data.SqlTypes (FYI) (If this comment was disrespectful, please report it.)
11/5/2003 3:26:20 PM:
Ian, you are the man. Thanks pal. My 4 hours of research has finally found a couple of minutes answer. Great job!! (If this comment was disrespectful, please report it.)
6/4/2004 4:40:04 PM:
in VB6 you should not use "IsNull" function...if you have many IsNull statements it will slow down your app...
instead of using IsNull in VB6, use the Len function. Check the Len for 0. The IsNull function checks the data-type of the value youre checking. That slows down the process.
Use this:
If Len(strVariable) > 0 then msgbox "Not Null" else msgbox "It is Null" end if
(If this comment was disrespectful, please report it.)
Hello, Im very impress in your accomplishment. Your a very good coder.You own this very usefull site. More power to you!!!!! greetings from www.naparansoft.cjb.net
(If this comment was disrespectful, please report it.)
7/24/2004 10:13:22 AM:
How about when inserting or updating a database row and some of the values are null. They are being converted to DBNull by our data provider (Sybase ASE ADO.NET native driver) and then when the command is executed, we are getting an error of (If this comment was disrespectful, please report it.)
10/20/2004 7:52:38 AM:
What Can I say except,
Thank you, thank you, thank you. (If this comment was disrespectful, please report it.)
When i use this verification to a MEMO field i have problems. This code resolve this problems... If Not (Convert.ToString(RS.Fields("Code").Value) = Convert.ToString(DBNull.Value)) Then CTBCode.Text = RS.Fields("Code").Value End If
(If this comment was disrespectful, please report it.)
7/5/2005 11:04:07 AM:
I keep getting an error on this when trying to check to see if a date field ISDBNULL. I'm getting the value from a stored procedure and works fine if the field is valued. Here is my error... Cast from type 'DBNull' to type 'Date' is not valid.
Any thoughts? (If this comment was disrespectful, please report it.)
10/13/2005 11:13:37 PM:
This has plagued me since the start of the betas!! Thanks buddy!! (If this comment was disrespectful, please report it.)
great (If this comment was disrespectful, please report it.)
Add Your Feedback
Your feedback will be posted below and an email sent to
the author. Please remember that the author was kind enough to
share this with you, so any criticisms must be stated politely, or they
will be deleted. (For feedback not related to this particular article, please
click here instead.)