Checking for NULL Value?

Posted on 2014-07-10
Medium Priority
Last Modified: 2014-07-11
I know this is an elementary question but I still get stuck on it and do it inadvertently at times, but when retrieving values from a database result set, how do you prevent a NULL Value Error. For example, if a Database Field is defined as a numeric but contains nothing, it will return a NULL value. How can I prevent an exception from happening. Because currently I'm getting the following error:

"Conversion from type 'DBNull' to type 'Integer' is not valid"
Question by:BlakeMcKenna
  • 3
  • 2
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40189321
You need to check for DBNull before you attempt to assign to a variable.


Dim defaultValue As Integer = -1
Dim x As Integer = If(dbReader("column") <> DBNull.Value, dbReader("column"), defaultValue)

Open in new window


Author Comment

ID: 40189340
Ok...this is what I had!

clsDE.rangeUnitType_ID = If (row("unitType_ID").ToString.Length = 0, DBNull.Value, row("unitType_ID"))

Open in new window


Author Comment

ID: 40189366

I just got the following error on the below line of code (see attached image).

        clsDE.serialNO = If(row.Cells(3).Value <> DBNull.Value, row.Cells(3).Value, "")

Open in new window

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 40189694
Hi BlakeMcKenna;

I believe this will give you the results you are looking for.

clsDE.serialNO = If(Not DBNull.Value.Equals(row.Cells(3).Value), row.Cells(3).Value, "")

Author Closing Comment

ID: 40190457
Thanks Fernando...that worked!
LVL 64

Expert Comment

by:Fernando Soto
ID: 40190803
Not a problem BlakeMcKenna, glad to help.

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question