0

gridview is placed in repeater, it is showing error on itemdata bound

code,

  <asp:Repeater ID="rptSearchResult" runat="server" 
                OnItemDataBound="rptSearchResult_ItemDataBound">
          <HeaderTemplate>
         <table border="0">
             <tr>
                 <td> Cost Page</td>
                 <td> Cost Page Description</td>
                 <td> Vendor Name</td>
                 <td> Bill Type</td>
             </tr>
      </HeaderTemplate>

      <ItemTemplate>
         <tr>
            <td> <%# Eval("CostPage")%></td>
               <td><%# Eval("CostPageDescription")%> </td>
               <td> <%# Eval("VendorName")%> </td>
               <td><%# Eval("BillType")%> </td>
         </tr>
           <tr>
            <td>
               <asp:GridView ID="gvDetails" runat="server" >
                   <Columns>  
                     <asp:TemplateField HeaderText="">
                     <ItemTemplate> 
                         <asp:CheckBox ID="chkSelect" runat="server" />
                     </ItemTemplate>
                    </asp:TemplateField>              
                     <asp:BoundField DataField="ItemId" HeaderText="Item ID"/>                    
                     <asp:BoundField DataField="ItemDescription"  HeaderText="Item Description"/>
                     <asp:BoundField DataField="BrandCode"   HeaderText="Brand Code"/>                    

                    </Columns> 

               </asp:GridView>  </td>
         </tr>
      </ItemTemplate>

      <FooterTemplate>
         </table>
      </FooterTemplate>

        </asp:Repeater>

in .cs file,

    protected void rptSearchResult_ItemDataBound(
       object sender, RepeaterItemEventArgs e)
    {
        GridView gd = (GridView)e.Item.FindControl("gvDetails");


        details e1 = new details();
        e1.itemid= 1;
        e1.itemdesc = "item1";
        e1.brandcode = "BBB";

        List<details> employees = new List<details>();
        employees.Add(e1);

        gd.DataSource = employees;
        gd.DataBind();

}

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
user2543573
  • 27
  • 1
  • 3
  • 7
  • 1
    Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 04 '13 at 16:56

2 Answers2

2

Check your gd object weather it is null or not than execute your certain conditions. Thus the code should be like below:

GridView gd = (GridView)e.Item.FindControl("gvDetails");

if(gd != null) {
  gd.DataSource = employees;
  gd.DataBind();
}

Edit: The error is coming because the name in the DataField="ItemDescription" of asp:BoundField doesn't match the datasource employees properties which you have specified at the time of databind. The markup code should be

<asp:BoundField DataField="itemdesc"  HeaderText="Item Description"/>                
Kundan Singh Chouhan
  • 13,952
  • 4
  • 27
  • 32
0

Resolved the issue by adding following check:

 if( (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) 
  {  
    //Non-null value for grid
  }

Now, the gridview is non-null value.

user2543573
  • 27
  • 1
  • 3
  • 7