0

i get NullReferenceException when fill Repeater.Some products features empty.i used IsNullControl() method when use UrlDecode() and kill() method for avoid exception.But i get error still.

   <asp:Repeater ID="rptProducts" runat="server">
        <ItemTemplate>
                <div>
                    <%# Eval("ProductName")%>
                </div>
                <div>
                    <%# kill(Server.UrlDecode(IsNullControl(Eval("ProductFeature").ToString())))%>
                </div>
        </ItemTemplate>
    </asp:Repeater>

    try
    {
        ProductsDataContext pdc = new ProductsDataContext();
        var query = from p in pdc.Products
                    select p;

        rptProducts.DataSource = query;
        rptProducts.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    public static string kill(string val)
    {
        val = val.Replace("<ul>", " ");
        val = val.Replace("<li>", " ");
        val = val.Replace("</li>", "<br/>");
        val = val.Replace("</ul>", " ");
        return val.ToString();
    }

    public static string IsNullControl(string val)
    {
        string space = " ";
        if (string.IsNullOrEmpty(val))
        {
            val = space;
        }
        return space;
    }
Pushkin
  • 63
  • 2
  • 8
  • Test the result of `IsNullControl`. Is it what you expect? – Hans Kesting May 25 '12 at 15:17
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:09

2 Answers2

2

You are converting a field into string first before checking for null in this code fragment --> Eval("ProductFeature").ToString()

Gone Coding
  • 88,305
  • 23
  • 172
  • 188
rt2800
  • 2,952
  • 2
  • 17
  • 26
1

check null as below

<%#kill(Server.UrlDecode(Eval("ProductFeature")?? String.Empty))%>

or you can change the query as below

 var query = from p in pdc.Products
             select p
             where p!= null;
Damith
  • 59,353
  • 12
  • 95
  • 149