Link to home
Start Free TrialLog in
Avatar of HLRosenberger
HLRosenbergerFlag for United States of America

asked on

Checking for NULL

If believe I've seen this done before, but how do I handle a NULL from a database on this statement:

Reader.Item(Reader.GetOrdinal("location"))

I know I could first do:

If Not IsDBNull(Reader.Item(Reader.GetOrdinal("location"))) then

but is there a way on the Reader statement to tell it to convert NULL to a space for text and 0 for numeric?  
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I usually add an Extension method to the project and use it like this

Textbox1.Text = IsStringNull(reader.item(1))
TextBox2.Text = IsIntNull(reader.item(2))

http://www.codeproject.com/KB/vb/BeginnerExtensionMethods.aspx

http://www.developer.com/net/vb/article.php/3679651/Implementing-Extension-Methods-in-VBNET.htm
in your sql query, you can do this:
select field1, field2, isnull(location, 0) as location from tablename
You may use:

IIF(IsDBNull(Expression) and (Not Information.IsNumeric(Expression)) , " ", IIF(Information.IsNumeric(Expression) and IsDBNull(Expression), 0 , defaultvalue)

Also, you can use Coalesce() in SQL which returns first Non-null value.
Avatar of HLRosenberger

ASKER

Thanks.  IsNull works fine for what I want.