0

I have a GridView to display my products and i have also made EditButton and DeleteButton to Update and Delete my products in the GridView. When I click on EditButton my gridview have a DropDownList to display my ProductType but i get an error with this message:

object reference not set to an instance of an object

This picture will show where i get that error:

enter image description here

And this is my method i use to fill datasource for my DropDownList:

enter image description here

Thank you for your help!

<asp:GridView ID="gridviewSanPham" runat="server" DataKeyNames="ID" AllowPaging="True" PageSize="4" AutoGenerateColumns="False" OnPageIndexChanging="gridviewSanPham_PageIndexChanging" OnRowCancelingEdit="gridviewSanPham_RowCancelingEdit" OnRowDeleting="gridviewSanPham_RowDeleting" OnRowEditing="gridviewSanPham_RowEditing" OnRowUpdating="gridviewSanPham_RowUpdating" OnRowDataBound="gridviewSanPham_RowDataBound">
        <Columns>
            <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowCancelButton="true" />
            <asp:CommandField ButtonType="Button" ShowDeleteButton="true" ShowCancelButton="true" />
            <asp:BoundField DataField="ID" HeaderText="Mã sản phẩm" ReadOnly="true"/>

            <asp:TemplateField HeaderText="Mã Loại">
                <ItemTemplate>
                    <%# Eval("TypeID") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label runat="server" ID="lblTypeID" Text='<%# Eval("TypeID") %>'></asp:Label>
                    <asp:DropDownList runat="server" ID="cboTypeID" ></asp:DropDownList> 
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Tên SP">
                <ItemTemplate>
                    <%# Eval("Name") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtName" Text='<%# Eval("Name") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Giá">
                <ItemTemplate>
                    <%# Eval("Price") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtPrice" Text='<%# Eval("Price") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mô tả">
                <ItemTemplate>
                    <%# Eval("Description") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox runat="server" ID="txtDescription" Text='<%# Eval("Description") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Hình"> 
                <ItemTemplate>
                    <%# Eval("Image") %>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label runat="server" ID="lblImage" Text='<%# Eval("Image") %>'></asp:Label>
                    <asp:FileUpload runat="server" ID="uploadImage" />
                </EditItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

That is my aspx code

binh nguyen
  • 127
  • 1
  • 13

2 Answers2

0

Put your Dopdownlist binding code in if ((e.Row.RowState & DataControlRowState.Edit) > 0) in RowDataBound event:

protected void gridviewSanPham_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // check if dropdownlist in edittemplate
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
           DropDownList drop = (DropDownList)e.Row.FindControl("cboTypeID");

           drop.DataSource = FillLoaiSP();
           drop.DataBind();
        }
    }
 }
-1

It will always throw an error because you are trying to find the dropdown in thegridviewSanPham_RowDataBound method. Just try to find the dropdown in the gridviewSanPham_RowEditing method. Because in RowDataBound event it will iterate through all rows, including the header and footer also, so it will throw a null object reference error in the case of the header and footer row.

Also your dropdown will be there in case of edit operation so either you can put filters for datarow and operation in the gridviewSanPham_RowDataBound method. But it's highly recommended that you bind the dropdown in the gridviewSanPham_RowEditing method.

danwellman
  • 9,068
  • 8
  • 60
  • 88
Aman Sahni
  • 212
  • 1
  • 8