Solved

check for null in Eval

Posted on 2009-04-03
15
830 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need to Modify a Script I found 5 77
.Net application crashing 6 21
Set form below another form 3 26
XML & .net 5 21
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now