Dealing with Null values returned by ADO.NET

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
Reply
This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available in our efforts to advance understanding of environmental, political, human rights, economic, democracy, scientific, and social justice issues, etc. We believe this constitutes a 'fair use' of any such copyrighted material as provided for in section 107 of the US Copyright Law. In accordance with Title 17 U.S.C. Section 107, the material on this site is distributed without profit to those who have expressed a prior interest in receiving the included information for research and educational purposes. For more information go to: http://www.law.cornell.edu/uscode/17/107.shtml . If you wish to use copyrighted material from this site for purposes of your own that go beyond 'fair use', you must obtain permission from the copyright owner.