Whats the best way to handle Null values stored in my database? I keep getting errors when I retrieve Null values from the database and try to bind or work with them in code, so how best should I deal with these...? I have a few ideas on how to cut down on this...
-Don't store Null values in the first place. I am using SQL server and I set all default values to 0 for numbers or '' for strings. I dpn't hknow how to deal with dates however so I leave those Null.
-Using a check to System.DBNull...either in code or as a set of common Functions. I currently do not employ this much. I have seen several versions of this though:
This which gives error:
Code:
If TypeOf objUser.DOB Is System.DBNull Then
'No Date
Else
'Return the Date
End If
Error:
Type Of...Is' requires its left operand to have a reference type, but this operand has the value type 'Date'(???)
and I came up with this, which might work:
Code:
If CType(objUser.DOB,Object) Is System.DBNull.Value Then
'No Date
Else
'Return the Date
End If
-Lastly, and I use this a LOT, is to use IsNull in my SQL. This is a horror to write and maintain. Instead of having:
Code:
SELECT * FROM tableUsers
I must use:
Code:
SELECT IsNull(UserName,'') As UserName, IsNull(FirstName,'') As FirstName, IsNull(LastName,'') As LastName, IsNull(DOB,'') As DOB etc. etc.
Any suggestions are appreciated!
-------------------------
Beware of programmers with screwdrivers...