• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

How To check Null values

I retrieve value from a database and save it in a variable , How Can I check if it is null value

Dim MyAccID As Integer = BillMasterTable.Rows(0).Item("AccID")

If MyAccID is Null  ' How To do that
0
Ahmadal_najjar2003
Asked:
Ahmadal_najjar2003
3 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
IsDBNull() function is what you need:

if IsDBNull (BillMasterTable.Rows(0).Item("AccID")) Then
  ...

Open in new window

0
 
appariCommented:
if isnothing(MyAccID )

or

if MyAccID  = dbnull.value
0
 
Stephen MandersonSoftware EngineerCommented:
If you are trying to trap errors when loading from a db this is how I do it.

I.e
Dim MyAccID As Integer = CheckForNullInteger(BillMasterTable.Rows(0).Item("AccID"))

    Public Function CheckForNullInteger(ByVal Value As Object) As Integer
        If Value Is System.DBNull.Value Then
        Else
            Return Value
        End If
    End Function
 
    Public Function CheckForNullDate(ByVal Value As Object) As DateTime
        If Value Is System.DBNull.Value Then
        Else
            Return Value
        End If
    End Function
 
    Public Function CheckForNullString(ByVal Value As Object) As String
        If Value Is System.DBNull.Value Then
            Return ""
        Else
            Return Value
        End If
    End Function

Open in new window

0
 
Ahmadal_najjar2003Author Commented:
Yes AngelIII, You touch the target.
Very good Functions from MrManderson.
And thanks to appari.
0
 
Ahmadal_najjar2003Author Commented:
Thanks To all.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Tackle projects and never again get stuck behind a technical roadblock.
Join Now