?
Solved

check for null in Eval

Posted on 2009-04-03
15
Medium Priority
?
843 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

765 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