I try to bind the dropdownlist in the GridView,but always get the result said:
Object reference not set to an instance of an object.
I set the breakpoint to follow the code,it showed I did get the data from the database,but when I follow the next code,the error showed.
My code:
Code:
cs code:
DropDownList ddlUpdateCategory = (DropDownList)this.GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].FindControl("ddlUpdateCategory");
using (NorthwindEntities ctx = new NorthwindEntities())
{
ddlUpdateCategory.DataTextField = "CategoryName";
ddlUpdateCategory.DataValueField = "CategoryID";
ddlUpdateCategory.DataSource = ctx.Categories;
ddlUpdateCategory.DataBind();
}
aspx code:
<asp:TemplateField HeaderText="CategoryName">
<EditItemTemplate>
<asp:DropDownList ID="ddlUpdateCategory" runat="server" DataTextField="CategoryName" DataValueField="CategoryID">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Thanks in advance!