Checking for NULL values

My blog has moved.
You can view this post at the following address:
http://www.osherove.com/blog/2003/7/28/checking-for-null-values.html
Published Monday, July 28, 2003 9:57 AM by RoyOsherove
Filed under:

Comments

Monday, July 28, 2003 12:36 AM by Paschal

# re: Checking for NULL values

Another solution, if you have a huge quantity of fields, is to do a concatenation with an empty string.

It works for me well in VB.

( I know it's not the 'cleanest' way :-))

myPhone = myDatareader("FieldPhone") + ""
Monday, July 28, 2003 3:11 AM by micho

# re: Checking for NULL values

sometimes i use "if typeof myDBNullObject is dbnull then"
Sunday, August 03, 2003 11:38 AM by Omer

# re: Checking for NULL values

With typed DataSets you can just use Is[Field]Null. e.g.: If you have a column named PersonId, you can use myRow.IsPersonIdNull.

Remember, this only works in typed DataSets.
Tuesday, January 06, 2004 10:33 AM by John Reyes

# re: Checking for NULL values

Hi there I have this line of code but it seems that it's ignoring the else statement. Any help will be greatly appreciated. Thanks.

If empRecord.IsNull("Email") Then
empRecord("Email") = "Not Available"
Else
empRecord("Email") = dataReader.GetString(dataReader.GetOrdinal("Email"))
End If
Monday, February 09, 2004 3:00 PM by MarcJ

# re: Checking for NULL values

One clean way to handle NULL values is to not bother checking for them, just convert them on the fly:

myString = Convert.ToString(myDataField("SomeString"))
myInteger = Convert.ToInt32(myDataField("SomeInteger"))

and so on.
Thursday, March 04, 2004 11:55 AM by g

# re: Checking for NULL values

fg
Friday, April 23, 2004 2:53 AM by a

# re: Checking for NULL values

if (cstr(rs3(1)) is null) then
a= 0
Response.Write a
end if
Thursday, June 10, 2004 7:05 PM by Thanks

# re: Checking for NULL values

Thanks MarcJ your solution is great!