Link to home
Start Free TrialLog in
Avatar of BlakeMcKenna
BlakeMcKennaFlag for United States of America

asked on

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"
Avatar of kaufmed
kaufmed
Flag of United States of America image

You need to check for DBNull before you attempt to assign to a variable.

e.g.

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

Open in new window

Avatar of BlakeMcKenna

ASKER

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

Ok,

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

Screenshot.jpg
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America 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
Thanks Fernando...that worked!
Not a problem BlakeMcKenna, glad to help.