Solved

check for null in Eval

Posted on 2009-04-03
15
835 Views
Last Modified: 2013-11-07
I have the following formview in asp.net to display quantities. How do I check for nulls? if its null, i want it to display 0
<asp:FormView ID="CapsuleFillingHourly" runat="server" 
            DataSourceID="HourlyLabel1">
                                                 
                                    <ItemTemplate>
    
    <table cellspacing="0" class="borderLine"  Width="300px">
            <tr>
                <td height="30px" class="borderLine" width="33%">
                    Cumulative <br />Total</td>
                <td height="30px" class="borderLine" width="33%">
                    Cumulative <br />Goal</td>
                <td height="30px" class="borderLine" width="33%" valign="middle">
                    Balance</td>
            </tr>
            <tr>
                <td height="30px"  class="borderLine"><asp:Label id="TotalActualLabel"  runat="server"  
                        Text='<%# String.Format("{0:##,###}", Eval("ActualTotal") )%>' /></td>
                <td  height="30px" class="borderLine"><asp:Label ID="TotalTargetLabel" runat="server" 
                        Text='<%# String.Format("{0:##,###}", Eval("TargetTotal") )%>' /></td>
                <td height="30px"  width="120px" id="CapsuleFillingHrDiv"><asp:Label ID="CapsuleFillingHourlyLabel"  runat="server" Text='<%# String.Format("{0:##,###}", Eval("Diff") )%>' /></td>
            </tr>
        </table>
       </ItemTemplate>
</asp:FormView>

Open in new window

0
Comment
Question by:sassy168
  • 8
  • 7
15 Comments
 
LVL 13

Expert Comment

by:sm394
ID: 24064649
<%# Eval("ActualTotal")??"0" )%>
0
 
LVL 13

Expert Comment

by:sm394
ID: 24064713
 or
  <%# (Eval("ActualTotal") == DBNull.Value) ? "0" : Eval("ActualTotal")%>
0
 

Author Comment

by:sassy168
ID: 24064998
what bout my original comma formatting?
0
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 13

Expert Comment

by:sm394
ID: 24065179
<%# (Eval("ActualTotal") == DBNull.Value) ? "0" :String.Format("{0:##,###}", Eval("ActualTotal") ) %>
0
 

Author Comment

by:sassy168
ID: 24079133
sm394: that didn't work...nothing showed up...(nothing before also, so i know the value is null)
0
 
LVL 13

Expert Comment

by:sm394
ID: 24079932
it works fine on my machine
0
 

Author Comment

by:sassy168
ID: 24080122
yeah it still doesn't come up...thats strange here is how i have it in my code
<asp:FormView ID="CapsuleFillingHourly" runat="server" 
            DataSourceID="HourlyLabel1">
                                                 
                                    <ItemTemplate>
    
    <table cellspacing="0" class="borderLine"  Width="300px">
            <tr>
                <td height="30px" class="borderLine" width="33%">
                    Cumulative <br />Total</td>
                <td height="30px" class="borderLine" width="33%">
                    Cumulative <br />Goal</td>
                <td height="30px" class="borderLine" width="33%" valign="middle">
                    Balance</td>
            </tr>
            <tr>
                <td height="30px"  class="borderLine"><asp:Label id="TotalActualLabel"  runat="server"  
                        Text='<%# (Eval("ActualTotal") == DBNull.Value) ? "0" :String.Format("{0:##,###}", Eval("ActualTotal") ) %>
' /></td>
                <td  height="30px" class="borderLine"><asp:Label ID="TotalTargetLabel" runat="server" 
                        Text='<%# String.Format("{0:##,###}", Eval("TargetTotal") )%>' /></td>
                <td height="30px"  width="120px" id="CapsuleFillingHrDiv"><asp:Label ID="CapsuleFillingHourlyLabel"  runat="server" Text='<%# String.Format("{0:##,###}", Eval("Diff") )%>' /></td>
            </tr>
        </table>
       </ItemTemplate>
</asp:FormView>

Open in new window

0
 
LVL 13

Expert Comment

by:sm394
ID: 24080309
not sure in that case i have used your code to test
<asp:Label id="TotalActualLabel"  runat="server"  
                        Text='<%# (Eval("ActualTotal") == DBNull.Value) ? "0" :String.Format("{0:##,###}", Eval("ActualTotal") ) %>
' />

and it works have a look at screen shot
Untitled.jpg
0
 

Author Comment

by:sassy168
ID: 24080414
i think i know what the problem is. The value I have on this record is actually 0. So if its 0, it wont do the formatting correctly so therefore it displays blank...how do i get around that?
0
 
LVL 13

Expert Comment

by:sm394
ID: 24080479
could you explain a bit more
0
 

Author Comment

by:sassy168
ID: 24080506
try putting a 0 instead of NULL in your sample application. I believe it doesn't work with String.Format("{0:##,###} when the value is 0.
0
 
LVL 13

Expert Comment

by:sm394
ID: 24080615
do you have any null values in ActualTotal
0
 

Author Comment

by:sassy168
ID: 24080695
actually i fixed it last week so its not null anymore. Only 0
0
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 24080933
in your code behind create this method
 public string GetPrice(object price)
        {
            string p= String.Format("{0:##,###}", price);
            return (p=="")?"0":p;
           
        }

in ASPX

<%# GetPrice(Eval("Price"))%>
0
 

Author Closing Comment

by:sassy168
ID: 31566461
perfect!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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