title: Gridview Footer Not an Instance
body: <p>I'm trying to get an Automatic ID filler for a gridview footer text box, but every time I run I get the "Not Referred Instance of an object" meaning the method isn't finding the Text box, Right?
  But I have the same text box in other method and is working correctly, what its wrong?</p>
  
  <p>Below is the code sample of GridView</p>
  
  <pre><code>   &lt;asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" AutoGenerateColumns="False" ShowFooter="True"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField HeaderText="ID UNIDAD"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBox1" runat="server" Text='&lt;%# Bind("ID_UNIDAD") %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:TextBox ID="txtAddID" runat="server" &gt;&lt;/asp:TextBox&gt;
                      &lt;/FooterTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lbl_ID" runat="server" Text='&lt;%# Bind("ID_UNIDAD") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField HeaderText="UNIDAD"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBox2" runat="server" Text='&lt;%# Bind("NOMBRE") %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:TextBox ID="txtNombre" runat="server" &gt;&lt;/asp:TextBox&gt;
                      &lt;/FooterTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lbl_Nombre" runat="server" Text='&lt;%# Bind("NOMBRE") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                     &lt;asp:TemplateField HeaderText="FRACCIONES"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:CheckBox ID="TextBox3" runat="server" Checked='&lt;%# Bind("FRACCIONES") %&gt;'&gt;&lt;/asp:CheckBox&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:CheckBox ID="chkFraccion" runat="server" &gt;&lt;/asp:CheckBox&gt;
                      &lt;/FooterTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:CheckBox ID="lbl_Fraccion" runat="server" Checked='&lt;%# Bind("FRACCIONES") %&gt;' &gt;&lt;/asp:CheckBox&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                     &lt;asp:TemplateField HeaderText="CLAVE SAT"&gt;
                      &lt;EditItemTemplate&gt;
                          &lt;asp:TextBox ID="TextBox4" runat="server" Text='&lt;%# Bind("CLAVE_SAT") %&gt;'&gt;&lt;/asp:TextBox&gt;
                      &lt;/EditItemTemplate&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:TextBox ID="txtClave" runat="server" &gt;&lt;/asp:TextBox&gt;
                      &lt;/FooterTemplate&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:Label ID="lbl_Clave" runat="server" Text='&lt;%# Bind("CLAVE_SAT") %&gt;'&gt;&lt;/asp:Label&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
                  &lt;asp:TemplateField&gt;
                      &lt;FooterTemplate&gt;
                          &lt;asp:ImageButton ID="Agregar" runat="server" ImageUrl="~/assets/iconos/add.ico" OnClick="Agregar_Click" /&gt;       
                      &lt;/FooterTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
              &lt;FooterStyle BackColor="#CCCCCC" /&gt;
              &lt;HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /&gt;
              &lt;PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" /&gt;
              &lt;RowStyle BackColor="White" /&gt;
              &lt;SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /&gt;
              &lt;SortedAscendingCellStyle BackColor="#F1F1F1" /&gt;
              &lt;SortedAscendingHeaderStyle BackColor="#808080" /&gt;
              &lt;SortedDescendingCellStyle BackColor="#CAC9C9" /&gt;
              &lt;SortedDescendingHeaderStyle BackColor="#383838" /&gt;
          &lt;/asp:GridView&gt; 
  </code></pre>
  
  <p>Below is the code sample for binding the grid in code behind </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Web.UI.HtmlControls;
  using System.Windows.Input;
  
  
  public partial class Default2 : System.Web.UI.Page
  {
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Integral Database"].ToString());
      string query;
      string countid = " ";
      SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["Integral Database"].ToString());
      string connStrr = ConfigurationManager.ConnectionStrings["Integral Database"].ConnectionString;
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
  
              llenagrid();
          IncrementoID();
      }
  
  
  
      public void llenagrid()
      {
        DataTable table = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStrr))
        {
            string sql = "SELECT id_unidad, nombre, fracciones, clave_sat from unidades";
              using (SqlCommand cmd = new SqlCommand(sql, conn))
              {
                  using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
                  {
                      ad.Fill(table);
                  }
             }
         }
        IncrementoID();
    GridView1.DataSource = table;
    GridView1.DataBind();        
      }
  
  
  
      protected void Agregar_Click(object sender, ImageClickEventArgs e)
      {
          connStr.Open();
          GridViewRow row = GridView1.FooterRow;
          TextBox txtNombre = (TextBox)row.FindControl("txtNombre");
          string str = "select count(nombre) from unidades where nombre like '"+txtNombre.Text+ "'";
          SqlCommand com = new SqlCommand(str,connStr);
          int count = Convert.ToInt32(com.ExecuteScalar());
          connStr.Close();
          if (count &gt; 0)
          {
              string script = "alert('Este Nombre ya Existe.','Atención');";
              ScriptManager.RegisterStartupScript(this, this.GetType(), "testScript", script, true);
          }
          else
          {
              connStr.Open();
              TextBox txtID = (TextBox)row.FindControl("txtAddID");
              CheckBox chkFracc = (CheckBox)row.FindControl("chkFraccion");
              TextBox txtClave = (TextBox)row.FindControl("txtClave");
              string ID = txtID.Text;
              string Nombre = txtNombre.Text;
              bool Fracc = chkFracc.Checked;
              string clave = txtClave.Text;
              string query2 = "Insert into unidades values('" + ID + "','" + Nombre + "','" + Fracc + "','" + clave + "')";
              SqlCommand cmd2 = new SqlCommand(query2, connStr);
              cmd2.ExecuteNonQuery();
              connStr.Close();
              llenagrid();
              IncrementoID();
          }
  
       }
  
  
      public void IncrementoID()
      {
          //query = "select Count(id_unidad) from unidades";
          //con.Open();
          //SqlCommand cmd = new SqlCommand(query, con);
          //string cid = cmd.ExecuteScalar().ToString();
          //con.Close();
          //TextBox ID = (TextBox)GridView1.FooterRow.FindControl("txtAddID");
          //ID.Text = countid + (int.Parse(cid) + 1);
          //ID.ReadOnly = true;
  
          con.Open();
          SqlCommand cmd = new SqlCommand("select MAX (CAST( ID_UNIDAD as INT)) from unidades",con);
          SqlDataReader rd = cmd.ExecuteReader();
          TextBox txtID = ((TextBox)GridView1.FooterRow.FindControl("txtAddID"));
          if (rd.Read())
          {
              string Value = rd[0].ToString();
              if (Value == "")
              {
  
                  txtID.Text = "1";
              }
              else
              {
                  txtID.Text = rd[0].ToString();
                  txtID.Text = (Convert.ToInt64(txtID.Text) + 1).ToString();
              }
          }
          con.Close();
      }
  
      }
  </code></pre>
  
  <p>As you can see, in the insert method it finds the gridview textbox (txtAddID) and when I try to use the same exact syntax in the IncrementoID Method it doesn't work.</p>