Dealing with Null values returned by ADO.NET
Posted: 3/9/2005 4:45:23 PM
By: Comfortably Anonymous
Times Read: 2,279
0 Dislikes: 0
Topic: Programming: .NET Framework
While a database can contain a null value, ASP.NET gets disturbed when you try to assign a null databse value to a string. Here's how to get around it:


Public Function IsDBNull(ByVal DBVal) As Boolean
     ' Used to check if value passed from Database is Null
     ' Returns True if passed value is Null
     ' Returns False is passed value is Not Null
     Return DBVal Is DBNull.Value
End Function

Public Function NullToString(ByVal DBVal) As String
   ' Returns empty string is value passed from database is Null
   ' Converts value to a string if Not Null
   If IsDBNull(DBVal) Then
        Return ""
   Else
        Return DBVal.ToString
   End If
End Function
Rating: (You must be logged in to vote)