DBNull and casting to string?

Joined
Jan 22, 2003
Messages
22
Location
Indiana, USA
Pretty easy, Im reading in a table that has some Null values scattered about. In binding the data to the grid I have an error because of the Nulls, cannot Cast Null value into a String. Yeah well that makes sense. But how can I call a function to check for the Null value and then populate the cell with " "?

Code:
<asp:Label ID="lblDateOut" Text=<%# TrimNull(Mid(DataBinder.Eval(Container.DataItem, "Date Out"),1,10)) %> Runat="server" EnableViewState="false" />
&
Code:
       Public Function TrimNull(ByVal sInput As String)
            If (sInput is System.DBNull.Value) Then
                TrimNull = " "
                return TrimNull
            Else
                TrimNull = sInput
                return TrimNull
            End If
        End Function
The above wont work because the Parameter for TrimNull is Null so I get the same error right? How can I do a logic check like this for DBNulls?

Many thanks.
 
you can use this function...
Code:
    Protected Function Nz(ByVal vValue As Object, ByVal vDefValue As Object) As Object
        If IsDBNull(vValue) Then
            Return vDefValue
        Else
            If vValue Is Nothing Then
                Return vDefValue
            Else
                Return vValue
            End If
        End If
    End Function

and call it this way

CType(Nz(drSqlReader.Item("SomeStringField"), ""), String)

or
CType(Nz(drSqlReader.Item("SomeNumericField"), ""), Integer)
 
Back
Top