Problem solve Get help with specific problems with your technologies, process and projects.

Null to datatype functions

Data from various sources may be null instead of the expected datatype (for example, when reading from a database or as ASP.NET query string).

This tip was submitted to the VS.NET Info Center by member Clive Chinery. Please let other users know how useful it is by rating it below. Do you have a tip or code of your own you'd like to share? Submit it here.


Data from various sources may be null instead of the expected datatype (for example, when reading from a database or as ASP.NET query string). Line 1 will crash if there no corresponding value in the query string, whereas line 2 works.

bFred = Request.QueryString("bNS")) 
bFred = NullToBoolean(Request.QueryString("bNS"))  

Code:

Public Function NullToBoolean(ByVal vVar As Object, Optional bDefault As
Boolean = False) As Boolean
'Purpose:
'   Convert null string to Boolean
'Input:
'   vVar    - Input variable
'Output:
'   Function returns 0 if vVar is null else value
  Try
   If IsDbNull(vVar) Then
    NullToBoolean = bDefault
   Else
    NullToBoolean = CBool(vVar)
   End If
  Catch
   NullToBoolean = False
  End Try
End Function
Public Function NullToCurrency(ByVal vVar As Object) As Decimal
'Purpose:
'   Convert null string to currency
'Input:
'   vVar    - Input variable
'Output:
'   Function returns 0 if vVar is null else value
 If IsDbNull(vVar) Then
  NullToCurrency = 0
 Else
  NullToCurrency = CDec(vVar)
 End If
End Function
Public Function NullToDate(ByVal vVar As Object) As Date
'Purpose:
'   Convert null string to currency
'Input:
'   vVar    - Input variable
'Output:
'   Function returns 0 if vVar is null else value
 If IsDbNull(vVar) Then
  NullToDate = CDate(YEAR_DOT)
 Else
  NullToDate = CDate(vVar)
 End If
End Function
Public Function NullToDateFormat(ByVal vVar As Object, Optional ByVal sFormat
As String = "dd/MMM/yyyy HH:mm") As String
'Purpose:
'   Convert null string to currency
'Input:
'   vVar    - Input variable
'   sFormat -
'Output:
'   Function returns 0 if vVar is null else value
 If IsDbNull(vVar) Then
  NullToDateFormat = ""
 Else
  NullToDateFormat = Format$(CDate(vVar), sFormat)
 End If
End Function
Public Function NullToInteger(ByVal vVar As Object) As Integer
'Purpose:
'   Convert null string to Integer
'Input:
'   vVar    - Input variable
'Output:
'   Function returns 0 if vVar is null else value
 If IsDbNull(vVar) Then
  NullToInteger = 0
 Else
  NullToInteger = CInt(vVar)
 End If
End Function
Public Function NullToString(ByVal vVar As Object) As String
'Purpose:
'   Convert null string to string
'Input:
'   vVar    - Input variable
'Output:
'   Function returns "" if vVar is null else value
  If IsDbNull(vVar) Then
    NullToString = ""
  Else
    NullToString = CStr(vVar)
  End If
End Function

Dig Deeper on .NET Framework database programming best practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close