title: 'System.NullReferenceException' on control in footer row of gridview
body: <p>I am trying to populate a drop down list in the footer row of a grid view. my mark up is as follows:</p>
  
  <pre><code>&lt;asp:Label ID="lblGrdImages" runat="server" &gt;&lt;/asp:Label&gt;
  &lt;asp:GridView 
      ID="grdImages" 
      runat="server"
      AllowPaging="true"
      ShowFooter="true"
      PageSize="5"
      AutoGenerateColumns="false"
      OnPageIndexChanging="grdImages_PageIndexChanging"
      OnRowCancelingEdit="grdImages_RowCancelingEdit"
      OnRowCommand="grdImages_RowCommand"
      OnRowEditing="grdImages_RowEditing"
      OnRowUpdating="grdImages_RowUpdating"
      OnRowDeleting="grdImages_RowDeleting" 
      EmptyDataText="No Data Available at this Time"       &gt;
      &lt;AlternatingRowStyle BackColor="White" ForeColor="#284775"&gt;&lt;/AlternatingRowStyle&gt;        
  
  
      &lt;Columns&gt;
  
          &lt;asp:TemplateField AccessibleHeaderText="Product ID" HeaderText="Product ID" FooterText="Product ID"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="lblProdId" runat="server" Text='&lt;%# Eval("pi.ProductId") %&gt;' &gt;&lt;/asp:Label&gt;
              &lt;/ItemTemplate&gt;
  
              &lt;EditItemTemplate&gt;
                  &lt;asp:Label ID="lblEditProdId" runat="server" Text='&lt;%# Eval("pi.ProductId") %&gt;' &gt;&lt;/asp:Label&gt;
              &lt;/EditItemTemplate&gt;
              &lt;FooterTemplate&gt;
                  &lt;asp:DropDownList ID="lstAddProdId" runat="server"  AppendDataBoundItems="true" &gt;
                      &lt;asp:ListItem&gt;Select a product&lt;/asp:ListItem&gt;
                  &lt;/asp:DropDownList&gt;
              &lt;/FooterTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField AccessibleHeaderText="Product Main Image" FooterText="Product Main Image" HeaderText="Product Main Image"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label ID="lblMainImgId" runat="server" Text='&lt;%# Eval("pi.ImageId") %&gt;' &gt;&lt;/asp:Label&gt;
                  &lt;asp:Image ID="imgMain" runat="server" Height="250" Width="250" ImageUrl='&lt;%# Eval("pi.ImagePath") %&gt;' /&gt;
              &lt;/ItemTemplate&gt;
              &lt;FooterTemplate&gt;
                  &lt;asp:FileUpload ID="flupMain" runat="server" AllowMultiple="false" /&gt;
              &lt;/FooterTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;asp:TemplateField AccessibleHeaderText="Supporting Images" FooterText="Supporting Images" HeaderText="Supporting Images"&gt;
              &lt;ItemTemplate&gt;
                  &lt;h2&gt;repeater here&lt;/h2&gt;
              &lt;/ItemTemplate&gt;
              &lt;FooterTemplate&gt;
                  &lt;asp:FileUpload ID="flupExtra" runat="server" AllowMultiple="true" /&gt;
              &lt;/FooterTemplate&gt;
          &lt;/asp:TemplateField&gt;
           &lt;asp:TemplateField HeaderText="Edit"&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:LinkButton ID="btnEdit" Text="Edit" runat="server" CommandName="Edit" /&gt;
                  &lt;br /&gt;
                  &lt;span onclick="return confirm('Are you sure you want to declare this product Discontinued?')"&gt;
                      &lt;asp:LinkButton ID="btnDelete" Text="Delete" runat="server" CommandName="Delete" /&gt;
                  &lt;/span&gt;
              &lt;/ItemTemplate&gt;
              &lt;EditItemTemplate&gt;
                  &lt;asp:LinkButton ID="btnUpdate" Text="Update" runat="server" CommandName="Update" /&gt;
                  &lt;br /&gt;
                  &lt;asp:LinkButton ID="btnCancel" Text="Cancel" runat="server" CommandName="Cancel" /&gt;
              &lt;/EditItemTemplate&gt;
              &lt;FooterTemplate&gt;
                  &lt;asp:Button ID="btnAddRecord" runat="server" Text="Add" CommandName="Add"&gt;&lt;/asp:Button&gt;
              &lt;/FooterTemplate&gt;
          &lt;/asp:TemplateField&gt;
      &lt;/Columns&gt;
  
      &lt;EditRowStyle BackColor="#999999"&gt;&lt;/EditRowStyle&gt;
  
      &lt;FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"&gt;&lt;/FooterStyle&gt;
  
      &lt;HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"&gt;&lt;/HeaderStyle&gt;
  
      &lt;PagerStyle HorizontalAlign="Center" BackColor="#284775" ForeColor="White"&gt;&lt;/PagerStyle&gt;
  
      &lt;RowStyle BackColor="#F7F6F3" ForeColor="#333333"&gt;&lt;/RowStyle&gt;
  
      &lt;SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333"&gt;&lt;/SelectedRowStyle&gt;
  
      &lt;SortedAscendingCellStyle BackColor="#E9E7E2"&gt;&lt;/SortedAscendingCellStyle&gt;
  
      &lt;SortedAscendingHeaderStyle BackColor="#506C8C"&gt;&lt;/SortedAscendingHeaderStyle&gt;
  
      &lt;SortedDescendingCellStyle BackColor="#FFFDF8"&gt;&lt;/SortedDescendingCellStyle&gt;
  
      &lt;SortedDescendingHeaderStyle BackColor="#6F8DAE"&gt;&lt;/SortedDescendingHeaderStyle&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p>My code behind function to populate the drop down list:</p>
  
  <pre><code>protected void lstProducts()
  {
      // find dropdownlist in the footer row of the gridview
      DropDownList prods = (DropDownList)grdImages.FooterRow.FindControl("lstAddProdId");
      // define ado.net objects
      SqlConnection con = new SqlConnection(connectionString);
      SqlCommand cmd = new SqlCommand("ProductDetails.bnc_ProductImages", con);
      cmd.CommandType = CommandType.StoredProcedure;
      SqlDataReader reader;
      // define the sp parameters
      cmd.Parameters.Add(new SqlParameter("@Status", SqlDbType.VarChar, 50));
      cmd.Parameters["@Status"].Value = "LstProds";
  
      try
      {
          con.Open(); // try to connect to db.
          reader = cmd.ExecuteReader();   // execut the reader
          while (reader.Read())
          {                
              ListItem item = new ListItem(); // create listitem
              item.Text = reader["p.ProductName"].ToString();   // add product name to item text
              item.Value = reader["p.ProductId"].ToString();    // add productId to item value
              prods.Items.Add(item);    // populate dropdown list.
          }
  
      }
      catch (Exception err)
      {
          lblGrdImages.Text = err.Message; // display error message in  a label
      }
      finally
      {
          con.Close();    // close the connection.
      }
  }
  </code></pre>
  
  <p>When I try to run it I get the following error:</p>
  
  <blockquote>
    <p>An exception of type 'System.NullReferenceException' occurred in App_Web_su5rfbqf.dll but was not handled in user code</p>
  </blockquote>
  
  <p>I am unsure as to why I am getting this exception. I am assuming it is not able to find the control in the grid view footer row but I do not know why. It may be a silly mistake somewhere but I cannot figure out what I should do</p>
  
  <p><strong>EDIT</strong> I am getting the error on this line of lstProducts():</p>
  
  <pre><code>DropDownList prods = (DropDownList)grdImages.FooterRow.FindControl("lstAddProdId");
  </code></pre>