Repeater Control Cast from type 'DBNull' to type 'Currency' is not valid

Hi I am trying to format a number on a aspx page in a reapeter item template.

<%# formatnumber(DataBinder.Eval(Container.DataItem, "TOTAL_SQ_FOOTAGE"),0)         %>

The problem is there are times when the value is null, and then the page bombs.

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


How do I put an if statement for checking for dbnull.value??  I need an if statement in other place as well.
LVL 1
macros14Asked:
Who is Participating?
 
Edwin_CConnect With a Mentor Commented:
Hi

Just in case you do not aware of the built-in formatting option,

<%# DataBinder.Eval(Container.DataItem, "TOTAL_SQ_FOOTAGE", {0:C})        %>

will show the number in currency format and handle null value nicely.

Edwin
0
 
bele04Commented:
Hi,

I'm assuming that you created that formatnumber method right? Why don't you check the value of the parameter that you are passing to your method first (whether it is null or not) before doing any formatting on it.  if it is null then just return a 0 value or whatever.

assuming your method looks something like this:

public string formatnumber(string value, int number)
{
     if(value == null)
         return "0"; //or something else

    //your code for formatting the value
}

-bele04-
0
 
Edwin_CCommented:
ooops, forgetting the double quote

<%# DataBinder.Eval(Container.DataItem, "TOTAL_SQ_FOOTAGE", "{0:C}")        %>
0
 
SKumar_1981Commented:
Try this
 Public Function IsSetNull() As Boolean
            Return Me.IsNull(Me.tablename.Column)
        End Function

Public Sub SetNull()
            Me(Me.tablename.Column) = System.Convert.DBNull
        End Sub
Regards
skumar
0
 
deanvanrooyenCommented:
you could also set the default value in the db to something more realistic like 0.00 - think about this - it will speed up the processing so you dont have to execute those extra lines of code - makes a difference if there are lots of requests.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.