2012-05-25 2 views
0

Repeater.Some 제품 기능 empty.i를 채울 때 NullReferenceException이 발생하면 예외를 피하기 위해 UrlDecode() 및 kill() 메서드를 사용할 때 IsNullControl() 메서드가 사용됩니다. 그러나 오류가 발생합니다.NullReferenceException이 발생합니다

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

다음과 같이 널 (null)>Eval("ProductFeature").ToString()

+0

'IsNullControl' 결과를 테스트하십시오. 그것은 당신이 기대하는 것입니까? –

+0

[NullReferenceException은 무엇이며 어떻게 수정합니까?] 가능한 복제본 (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine

답변

2

먼저이 코드 조각에 널 (null)을 점검하기 전에 문자열로 필드를 변환하는

var query = from p in pdc.Products 
      select p 
      where p!= null; 
1

확인하거나 아래 쿼리를 변경할 수 있습니다 -

<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; 
    }