56

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?

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191

12 Answers12

69

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.

Jason Snelders
  • 5,471
  • 4
  • 34
  • 40
  • 9
    I did the check in the <%# %> tags and == null didn't identify the null returns; I had to use == DBNull.Value. – user467384 Jul 16 '13 at 20:31
  • Where would I add that to my code here: http://stackoverflow.com/questions/27237555/why-empty-cell-throws-an-error-during-sql-stored-procedure-execution – SearchForKnowledge Dec 01 '14 at 21:33
  • Worth an additional check to see if myValue.ToString() is empty as well. For example, Eval("MyDate", "{0:d}") gives an empty string if MyDate is a nullable DateTime object. – Kevin Shea Mar 21 '17 at 11:34
51

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>
HasanG
  • 12,734
  • 29
  • 100
  • 154
13

I use the following for VB.Net:

<%# If(Eval("item").ToString() Is DBNull.Value, "0 value", Eval("item")) %>
Jonty
  • 662
  • 7
  • 8
  • Where would I add that to my code here: http://stackoverflow.com/questions/27237555/why-empty-cell-throws-an-error-during-sql-stored-procedure-execution – SearchForKnowledge Dec 01 '14 at 21:34
4

It should work as well

Eval("item") == null?"0": Eval("item");
COD3BOY
  • 11,964
  • 1
  • 38
  • 56
3

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

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

1

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>")%>
Prakash
  • 11
  • 1
1

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

Kristopher Ives
  • 5,838
  • 7
  • 42
  • 67
1

Used a modified version of Jason's answer:

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

  return myValue.ToString();
}
1

Use IIF.

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

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

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I haven't tested your code, but "<%# If(Eval("item"), "0 value") %>" looks a bit odd. Not sure you can actually use an If statement with Eval like that, and wouldn't the result just be a True/False returned? – Jason Snelders Dec 30 '09 at 12:09
  • No, that's the VB.NET binary `If` operator: `If(value, valueIfNull)`: http://msdn.microsoft.com/en-us/library/bb513985.aspx – Heinzi Dec 30 '09 at 12:23
0

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

'<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'
Alicia
  • 1,152
  • 1
  • 23
  • 41
Ziyad Godil
  • 2,620
  • 1
  • 14
  • 36
0

You can use the following for VB.net, especially if the value is a boolean:

<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>

For example, use this to automatically check or uncheck a checkbox with inline IIF eval:

<asp:CheckBox ID="mycheckbox" runat="server" Checked='<%# IIf(Eval("mydata").Equals(DBNull.Value), 0, Eval("mydata"))%>' />

The other method with .ToString will cause an error trying to convert the DBnull to a boolean.

Jeff
  • 1,362
  • 14
  • 17