Theomax
Theomax

Reputation: 6792

ASP.NET User control Eval() DB.Null problem

I have a user control that contains a repeater. The repeater outputs either one or two columns based on the parameters used when an instance of it is created.

If a value is not specified for the second column i.e only one column should be displayed, it doesn't seem to like it if a value hasn't been set.

I'm trying to output a string that is set in the user control code behind, not a data item.

I have the following aspx:

<%# Eval(Column2Data) == DBNull.Value ? "</tr> " : String.Format("<td>{0}</td></tr>", Eval(Column2Data))%>

If Column2Data has a value, it works but if it doesn't the following error is displayed:

Value cannot be null.
Parameter name: expression

Any ideas why this isn't working?

Upvotes: 0

Views: 2219

Answers (4)

Jithin John
Jithin John

Reputation: 17

<%# (string.IsNullOrEmpty(Convert.ToString(Eval("Column2Data")))) ? "Value" :Eval("Column2Data") %>

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

Try this,

 <%# Eval("Column2Data")==null  ? "<tr><td>N.A</td></tr>" 
                 : Eval("Column2Data","<tr><td>{0}</td></tr>")%>

This is the sample List,

public class Data
    {
        public int? Column2Data { get; set; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            List<Data> list = new List<Data>()
            {
                 new Data(){ Column2Data=10100},
                 new Data(){},
                 new Data(){ Column2Data=4000}
            };
            Repeater1.DataSource = list;
            Repeater1.DataBind();
        }
    }

Upvotes: 1

ScottE
ScottE

Reputation: 21630

This is what you're looking for:

<%#  DBNull.Value.Equals(Eval(Column2Data)) ? "</tr> " : String.Format("<td>{0}</td></tr>", Eval(Column2Data))%>

Upvotes: 2

Gregory Nozik
Gregory Nozik

Reputation: 3374

Try this one

<% (Column2Data == null) ? "</tr>td></td></tr>", Eval(Column2Data))%> 

If value is null I think you want to present empty td

Upvotes: 2

Related Questions