Cast from type 'DBNull' to type 'String' is not valid.

I have seen this question many times in the ASP.NET forums and thought that it would be a good idea to blog about.
When you are working with functions in the HTML of your DataGrid and passing the value (which can be also NULL) from the database to a function, which accepts a string, then you will get such an error.

Cast from type 'DBNull' to type 'String' is not valid.

To get this working you will have to pass the value as an object. That means the function must simply accept it as an object instead of a string. Something like this:

<asp:datagrid id="datagrid".....>
<Columns>
<asp:TemplateColumn ...>
  <ItemTemplate>
    <%# DoSomething(Container.DataItem("TheValue")) %>
  </ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

And in your Codebehind:

function public DoSomething(Dim obj as Object)
  If not IsDBNull(obj) then
  ' Do the processing here...
  End If
end function


Sonu

Comments

# Jerry Pisk said:

You don't have to pass it as object. Simply use the as operator, like this:

DoSomething(Container.DataItem("TheValue") as string);

and check for null in DoSomething.

Tuesday, December 21, 2004 6:28 PM
# David Silverlight said:

Interesting approach. Thanks for the suggestion. If it works, I might change my approach. I normally handle nulls a bit differently, though, but I might try this approach. I would expect that it would complain if you added "as string" the same way it would if you passed a null value to a string parameter. Sonu, did this do the trick?

Tuesday, December 21, 2004 9:34 PM
# Sonu Kapoor said:

Unfortunaly not David. The approach which Jerry doesnt work for me.

Tuesday, December 21, 2004 9:40 PM
# Sonu Kapoor said:

* which Jerry described...

Tuesday, December 21, 2004 9:40 PM
# Mike said:

Great Tip!!!

Tuesday, December 28, 2004 2:35 PM
# Chris said:

Why not handle the null on in the Database query? This way any controls that use the query are handled...

Thursday, January 13, 2005 12:39 AM
# gridview said:

Jerry your approach doesnt work for me.

Wednesday, June 27, 2007 3:15 AM
# Brendan said:

You could handle the null on the query side by using a case statement in your select statement.  In this manor, if the datum is null, then simply return a more desirable value, such as an empy string.

Monday, July 30, 2007 1:04 PM
# E C said:

Brendan, Could you give an example of how that would look in the query?

Monday, August 13, 2007 4:32 PM
# Nick Corley said:

I ran into a similar problem where the data item was a dbnull and was able to use "as string" like this:

Convert.ToDouble(row["DoubleRow"] as string)

Friday, August 17, 2007 12:30 PM
# WebMonkey-in-Ireland said:

I wish MS had built this in...

txtAddress1.Text = objReader.Item("Address1")

If something like this is NULL it throws an error which is needless. Simply have a property to do a silent conversion to string.

Thursday, December 13, 2007 5:53 AM
# yo said:

Convert.ToString(databaseobject)

Tuesday, May 06, 2008 10:00 AM
# rareddy said:

If you're using a Dataset, just tell the designer to return any NULL values as Empty.

Friday, May 09, 2008 1:28 PM
# SomeOne said:

You can do row["DoubleRow"].ToString() and it will produce String.Empty if the value is DBNull.

Tuesday, March 03, 2009 5:12 PM
# Seppe said:

Dude, thanks a lot, you solved a big headache!!!!

Wednesday, October 28, 2009 3:06 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)