Checking for NULL Value?

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"
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
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

BlakeMcKennaAuthor Commented:
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

BlakeMcKennaAuthor Commented:

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

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Fernando SotoRetiredCommented:
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, "")

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Thanks Fernando...that worked!
Fernando SotoRetiredCommented:
Not a problem BlakeMcKenna, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.