0
--code behind--

 protected void findcourse()
    {
        foreach (GridViewRow grow in GridView2.Rows)
        {
            DropDownList drop1 = (DropDownList)grow.FindControl("DropDownList1");

            con.Open();
            SqlCommand cmd = new SqlCommand("select descr from restrnt_master", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dt = new DataSet();
            da.Fill(dt);
            con.Close();
            drop1.DataSource = dt;
            drop1.DataTextField = "descr";
            drop1.DataBind();
            drop1.Items.Insert(0, new ListItem("--Select--", "0"));
        }

    }

--source code--

  <asp:TemplateField HeaderText="Item">
                                    <ItemTemplate>
                                        <asp:Label ID="Label8" runat="server" Text='<%# Eval("item") %>'></asp:Label>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                                            SelectedValue='<%# Eval("item") %>'
                                            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                                        </asp:DropDownList>


                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

                                            onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                                        </asp:DropDownList>
                                        <br />

                                    </FooterTemplate>
                                </asp:TemplateField>

This shows the following error when page is loaded.kindly help to identify the error. I have added the findcourse() function in the page load and after insert,update codings

System.NullReferenceException: Object reference not set to an instance of an object.

shanifa S
  • 37
  • 5
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ProgrammingLlama Nov 29 '17 at 07:51

2 Answers2

1

You must be getting error in this line

DropDownList drop1 = (DropDownList)grow.FindControl("DropDownList1");

it is because your control is in edit and footer template and you need to bind it separately.

There is an alternate solution,get your datatable and bind it in edit and footer template in aspx using DataSource.

 protected DataTable findCourse()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select descr from restrnt_master", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet dt = new DataSet();
        da.Fill(dt);
        con.Close();

        return dt;
    }



<EditItemTemplate>
                                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSource="<%# findCourse() %>"
                                        SelectedValue='<%# Eval("item") %>'
                                        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
                                    </asp:DropDownList>


                                </EditItemTemplate>

<FooterTemplate>
                                    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" DataSource="<%# findCourse() %>" 

                                        onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                                    </asp:DropDownList>
                                    <br />

                                </FooterTemplate>
Mohammad
  • 132
  • 5
1

Solved..

I just removed the Selectedvalue (SelectedValue='<%# Eval("item") %>') attribute from DropDownList1..and its working..

shanifa S
  • 37
  • 5