Casting Null values

  • Thread starter Thread starter Brad Allison
  • Start date Start date
B

Brad Allison

I need some simple help. I am filling a dataset with a table (no relations) and then using a dataview's find method to find one record. Each record may contain up to six dates. When I get to a record that comes back with a NULL date, I get the following error:

Additional information: Cast from type 'DBNull' to type 'String' is not valid.

The line of code where this is happening is here:
txtUWPCH.Text = dvDogInfo(idxFound).Item("WPCHEarned")

If somebody can shed some light on using DBNull or whatever other process or logic I need thanks in advance.

Brad
 
You can use some sort of helper function to convert from DBNull to String,
for example:

Public Function StringNullHelper(obj As Object) As String
If IsDbNull(obj) Then Return String.Empty
Return obj.ToString()
End Function

And then use it like:

txtUWPCH.Text = StringNullHelper(dvDogInfo(idxFound).Item("WPCHEarned"))

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist



I need some simple help. I am filling a dataset with a table (no relations)
and then using a dataview's find method to find one record. Each record may
contain up to six dates. When I get to a record that comes back with a NULL
date, I get the following error:

Additional information: Cast from type 'DBNull' to type 'String' is not
valid.

The line of code where this is happening is here:
txtUWPCH.Text = dvDogInfo(idxFound).Item("WPCHEarned")

If somebody can shed some light on using DBNull or whatever other process or
logic I need thanks in advance.

Brad
 
Back
Top