Tuesday, November 15, 2011

Code For Null Value Exception in vb.net

This is the code for null value exception in vb.net :-

Enum enumObjectType
StrType = 0
IntType = 1
DblType = 2
End Enum

Public Function CheckDBNull(ByVal obj As Object, _
Optional ByVal ObjectType As enumObjectType = enumObjectType.StrType) As Object
Dim objReturn As Object
objReturn = obj
If ObjectType = enumObjectType.StrType And IsDBNull(obj) Then
objReturn = ""
ElseIf ObjectType = enumObjectType.IntType And IsDBNull(obj) Then
objReturn = 0
ElseIf ObjectType = enumObjectType.DblType And IsDBNull(obj) Then
objReturn = 0.0
End If
Return objReturn
End Function

you just need to call "CheckDBNull()" Function

like eg:

"If (CheckDBNull(Session("test")) = "") Then

End If