Search code examples
c#asp.netdatabinder

DataBinder.Eval data item Empty


I am having trouble trying to figure on how to hide a ',' when the DataItem is empty.

Basically, I have table row that displays Registered Address and populated using the following technique:

<%# DataBinder.Eval(Container.DataItem, "Address_Line_1" )%>,
<%# DataBinder.Eval(Container.DataItem, "Address_Line_2")%>,
<%# DataBinder.Eval(Container.DataItem, "TOWNLAND")%>,                          
<%# DataBinder.Eval(Container.DataItem, "CITY")%>,
<%# DataBinder.Eval(Container.DataItem, "STATE")%>,

Now if one or many of the above DataItem comes back as Empty then on the front end it is displayed as

Address 1, , , CITY, ,

I tried the following to hide the comma (',') but I keep getting an error saying

Missing ')' on the first line of 'If' statement

<%#IIf(IsDBNull(DataBinder.Eval(Container.DataItem, "STATE")) OrElse 

String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "STATE")) , "" , 

DataBinder.Eval(Container.DataItem, "STATE") & ",")%>

I am not sure whether my 'if' statement is wrong or It can't be done as above?

Anybody have suggestions about the above or any other alternative way of hiding the comma if a NULL value?


Solution

  • This should work:

    <%# DataBinder.Eval(Container.DataItem, "Address_Line_1") != null && !String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "Address_Line_1").ToString()) ? DataBinder.Eval(Container.DataItem, "Address_Line_1").ToString() + "," : "" %>
    

    condition ? true : false