Using '<%# Eval("item") %>'; Handling Null Value and showing 0 against

asp.netDataitem

asp.net Problem Overview


If dataitem is Null I want to show 0

<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label>

How can I accomplish this?

asp.net Solutions


Solution 1 - asp.net

You can also create a public method on the page then call that from the code-in-front.

e.g. if using C#:

public string ProcessMyDataItem(object myValue)
{
  if (myValue == null)
  {
     return "0 value";
  }

  return myValue.ToString();
}

Then the label in the code-in-front will be something like:

<asp:Label ID="Label18" Text='<%# ProcessMyDataItem(Eval("item")) %>' runat="server"></asp:Label>

Sorry, haven't tested this code so can't guarantee I got the syntax of "<%# ProcessMyDataItem(Eval("item")) %>" entirely correct.

Solution 2 - asp.net

I'm using this for string values:

<%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%>

You can also use following for nullable values:

<%#(Eval("Data") == null ? "0" : Eval("Data"))%>

Also if you're using .net 4.5 and above I suggest you use strongly typed data binding:

<asp:Repeater runat="server" DataSourceID="odsUsers" ItemType="Entity.User">
    <ItemTemplate>
        <%# Item.Title %>
    </ItemTemplate>
</asp:Repeater>

Solution 3 - asp.net

I use the following for VB.Net:

<%# If(Eval("item").ToString() Is DBNull.Value, "0 value", Eval("item")) %>

Solution 4 - asp.net

It should work as well

Eval("item") == null?"0": Eval("item");

Solution 5 - asp.net

Moreover, you can use (x = Eval("item") ?? 0) in this case.

http://msdn.microsoft.com/en-us/library/ms173224.aspx

Solution 6 - asp.net

I don't know ASP.NET very well, but can you use the ternary operator?

http://en.wikipedia.org/wiki/Ternary_operation

Something like: (x=Eval("item")) == Null ? 0 : x

Solution 7 - asp.net

try this code it might be useful -

<%# ((DataBinder.Eval(Container.DataItem,"ImageFilename").ToString()=="") ? "" :"<a
 href="+DataBinder.Eval(Container.DataItem, "link")+"><img
 src='/Images/Products/"+DataBinder.Eval(Container.DataItem,
 "ImageFilename")+"' border='0' /></a>")%>

Solution 8 - asp.net

Used a modified version of Jason's answer:

public string ProcessMyDataItem(object myValue)
{
  if (myValue.ToString().Length < 1)
  {
     return "0 value";
  }

  return myValue.ToString();
}

Solution 9 - asp.net

Try replacing <%# Eval("item") %> with <%# If(Eval("item"), "0 value") %> (or <%# Eval("item") ?? "0 value" %>, when using C#).

Solution 10 - asp.net

Use IIF.

<asp:Label ID="Label18" Text='<%# IIF(Eval("item") Is DBNull.Value,"0", Eval("item") %>' 
runat="server"></asp:Label>

Solution 11 - asp.net

I have tried this code and it works well for both null and empty situations :

'<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammad AkhtarView Question on Stackoverflow
Solution 1 - asp.netJason SneldersView Answer on Stackoverflow
Solution 2 - asp.netHasanGView Answer on Stackoverflow
Solution 3 - asp.netJontyView Answer on Stackoverflow
Solution 4 - asp.netMuhammad MudassirView Answer on Stackoverflow
Solution 5 - asp.netAlexei PshenichnyiView Answer on Stackoverflow
Solution 6 - asp.netKristopher IvesView Answer on Stackoverflow
Solution 7 - asp.netPrakashView Answer on Stackoverflow
Solution 8 - asp.netDavid GauthierView Answer on Stackoverflow
Solution 9 - asp.netHeinziView Answer on Stackoverflow
Solution 10 - asp.netGubiView Answer on Stackoverflow
Solution 11 - asp.netZiyad GodilView Answer on Stackoverflow