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

Posted on 2006-06-01
Medium Priority
Last Modified: 2012-08-14
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.
Question by:macros14

Expert Comment

ID: 16813011

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

LVL 16

Accepted Solution

Edwin_C earned 2000 total points
ID: 16813095

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.

LVL 16

Expert Comment

ID: 16813100
ooops, forgetting the double quote

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

Expert Comment

ID: 16813535
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
LVL 12

Expert Comment

ID: 16813710
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.

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question