I usually create a function like this:
Function SafeString(ByVal o As Object) As String
Dim s As String = String.Empty
If Not (o Is Nothing OrElse o Is DBNull.Value) Then
s = Convert.ToString(o)
End If
Return (s)
End Function
This function returns an empty string if you pass it an DBNull.Value. Also
note that the object is first checked to make sure it is not Nothing. If
you're pulling data from a SQL Server, you can use the COALESCE() function
on the Server-Side to perform a similar function before it ever gets to your
client app.
Thanks,
Mike C
"Hon Yuen, Ng" <hy**@tm.net.my> wrote in message
news:eA**************@TK2MSFTNGP14.phx.gbl...
Hi
This is a newbie question.
I've a datatable that contains null value. Sometime i need to store this
data into different variables. However, error will be thrown if the data i
tried to stored in the variable is a null value.
E.g.
Dim int As Int64
Dim null As Object
null = DBNull.Value
int = null 'this will throw error
To solve this, i have to check if the value is DBNull each time. Is there
a simpler way to do this?
Thanks in advance.
From,
Hon Yuen, Ng