[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

check for null in Eval

Posted on 2009-04-03
15
Medium Priority
?
845 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 2000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

650 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