Вопрос

***ERROR***
***********
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
***********
Line: <td>< asp:Label ID="Label59" runat="server" Text='<%# Eval("field2").Equals("") ? "" : Eval("field3") + " " + Eval("field4") + " " + Eval("field 5") %>' /></td>
***********
***********

Hi,

Appreciate being able to get any feedback. I'm somewhat new to ASP.NET using 3.5

Getting error after adding a new field to an existing sqlreader class. The error is supposedly pointing to a null value in a datalist. But if this one line is removed there is no problem for the entire ItemTemplate.

_var= reader["field"] != null ? (int)reader["field"] : 0;

The only other change to the sqlreader class is the new field's get set.

Thank you in advance

Это было полезно?

Решение

If your field2 contains null, Eval("field2") will be null so you'll get a null-ref while calling Equals on it. Simply swap the operands:

<%# "".Equals(Eval("field2")) ? "" : Eval("field3") + " " + Eval("field4") + " " + Eval("field 5") %>

Although I'd recommend comparing with null which is more readable and likely more correct.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top