NullToBoolean, NullToCurrency, NullToDate, NullToInteger, NullToString : VB.NET All Null to datatype functions
Found this very helpfull, just to share.
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
[source searchvb]
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
[source searchvb]
Tags: