Link to home
Start Free TrialLog in
Avatar of macros14
macros14

asked on

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.
Avatar of bele04
bele04

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-
ASKER CERTIFIED SOLUTION
Avatar of Edwin_C
Edwin_C
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ooops, forgetting the double quote

<%# DataBinder.Eval(Container.DataItem, "TOTAL_SQ_FOOTAGE", "{0:C}")        %>
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
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.