Search code examples
c#displayformat

display format string for DBNull not working as expected


Hi all I have applied display format string for a label as follows

<dx:ASPxLabel ID="lblPrice" runat="server" Text='<%#Eval("Price")!=DBNull.Value? string.Format("{0:c}", Eval("Price")) :string.Format("{0:c}","0.00") %>' />

This is displaying the amount with $ symbol when it is having data but when null for 0.00 it is not displaying the $ symbol can some one help me


Solution

  • You need to pass some numeric literal into Format method, so that the currency format {0:c} can be applied, but you are passing string literal "0.00". Try to change "0.00" literal to either 0.0 or 0 or 0.00, choose the most readable literal for you.

    write

    string.Format("{0:c}", 0.00) //returns $0.00. 
    //The same result for any numeric zero literal
    

    instead of

    string.Format("{0:c}", "0.00") //returns 0.00
    

    You can also simplify code a bit to:

    <%# string.Format("{0:c}", Eval("Price") != DBNull.Value ? Eval("Price") : 0 ) %>