Checking for NULL Value?

Posted on 2014-07-10
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
    LVL 74

    Expert Comment

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

    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


    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

    LVL 62

    Accepted Solution

    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

    Thanks Fernando...that worked!
    LVL 62

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now