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

Posted on 2006-06-01
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
    LVL 9

    Expert Comment


    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


    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

    ooops, forgetting the double quote

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

    Expert Comment

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now