Wednesday, August 31, 2005

Convert to Int32 handling NULL

Public Shared Function ConvertToInt32(ByVal value As Object) As Int32
    Try
        If (value Is Nothing) OrElse (value Is DBNull.Value) Then
            Return 0
        Else
            Return Convert.ToInt32(value)
        End If
    Catch
        Return 0
    End Try
End Function

Public Shared Function ConvertToInt32(ByVal value As String) As Int32
    Try
        If (value Is Nothing) OrElse (value Is DBNull.Value) Then
            Return 0
        Else
            Return Convert.ToInt32(value)
        End If
    Catch
        Return 0
    End Try
End Function

No comments: