How to test for Null values in ADODB recordset in VB.Net RRS feed

  • Question

  • What is the easiest way to test for a Null value in an ADODB recordset when using VB.Net.

    The IsNull function does not work in VB.Net, or so it seems.



    Friday, September 15, 2006 4:09 PM


  • IsDBNull() replaces the isNull function in VB

    Friday, September 15, 2006 5:11 PM

All replies

  • I believe it should be DBNull.Value

    Even though to you and me its still "null" - its a different type value meaning pretty much the same thing generally

    does this help?

    Friday, September 15, 2006 4:21 PM
  • I tried it as follows

    Dim nulltest as DBNull

    nulltest = DBNull.Value

    if rs.fields("Tonne").value = nulltest then

    but I get the following error

    Operator '=' not defined for types System.DBNull and Object

    Friday, September 15, 2006 4:44 PM
  • yes you can't do that. It should be:


    if rs.fields("Tonne").Value = DBNull.Value then

       'whatever here

    end if

    Friday, September 15, 2006 5:00 PM
  • IsDBNull() replaces the isNull function in VB

    Friday, September 15, 2006 5:11 PM
  • probably uses DBNull.Value in the function ;-)
    Friday, September 15, 2006 5:22 PM

    If ( Not Convert.IsDBNull( dr("FirstName") ) ) Then

    Me._firstName = Cstr( dr("FirstName") )

    End If

    Thursday, August 23, 2007 8:48 PM
  • Fuc...g  AWESOME  Udhai....
    • Proposed as answer by Cyber Ghost Tuesday, August 4, 2009 8:15 PM
    Wednesday, March 25, 2009 8:56 AM
  • Ok , took me a while to get logged on here but here is the best way of doing it.

    Dim NULL as System.DBNull = System.DBNull.Value

    name = iif(rs(0).value is null,"Unknown",rs(0).value)


    qty = iif(rs(1).value is NULL ,0,rs(1).value)

    this can also be done replacing the rs(0).value with the name of the actual field as indicated above "FirstName"

    This way, the value returned will always be validated against a null value, and in such event, the null value will then be replaced by "Unknown" or 0 but in the event of there not being a null value, the actual value will be returned.


    sorry for the late reply


    Tuesday, August 4, 2009 8:21 PM
  • handling dbnull in vb.net - full source code





    Wednesday, September 14, 2011 4:36 PM
  • this work for me:


    Thursday, October 4, 2018 5:18 PM