Solved

check for null in Eval

Posted on 2009-04-03
15
832 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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