title: Update Database From Check Box
body: <p>I have a <code>GridView</code> with a <code>ItemTemplate</code> field that is capturing the <code>ID</code> of the checkbox that is checked, and I have <code>AutoPostBack=false</code> so that it is not updated immediately, and I have a <code>asp:Button</code> that I want to use <code>C#</code> to run an update statement to update a database with all of the <code>ID's</code> from the checked checkboxes.  I am setting things up but I keep getting an error of</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>when the button is pressed, more specifically it is this line of code that throws the error:</p>
  
  <pre><code>string dbrowtoupdate = ((Label)row.FindControl("dataid")).Text;
  </code></pre>
  
  <p>And this is the HTML\C# for my grid, it's source is a SQL Query and it populates perfectly.</p>
  
  <pre><code>&lt;tr&gt;
  &lt;td valign="top"&gt;
      &lt;asp:GridView runat="server" ID="gridviewtrythis" AutoGenerateColumns="false"  &gt;
          &lt;Columns&gt;
          &lt;asp:BoundField DataField="Name" HeaderText="Full Name" /&gt;
          &lt;asp:TemplateField&gt;
              &lt;ItemTemplate&gt;
                  &lt;asp:Label runat="server" Text='&lt;%#Eval("dataid") %&gt;'   ID="dataid" Visible="false"&gt;&lt;/asp:Label&gt;
               &lt;/ItemTemplate&gt;
               &lt;ItemTemplate&gt;
                  &lt;asp:CheckBox ID="cbox" runat="server" AutoPostBack="false" Checked='&lt;%# Convert.ToBoolean(Eval("cbox")) %&gt;' /&gt;
              &lt;/ItemTemplate&gt;
          &lt;/asp:TemplateField&gt;
          &lt;/Columns&gt;
      &lt;/asp:GridView&gt;                                
          &lt;asp:Button runat="server" ID="btnUpdateDB" Text="Add" OnClick="btnUpdateDB_Click" /&gt;
  &lt;/td&gt;
  &lt;/tr&gt;
  
  protected void btnUpdateDB_Click(object sender, EventArgs e)
  {
  try
  {
      foreach (GridViewRow row in gridviewtrythis.Rows)
      {
          string dbrowtoupdate = ((Label)row.FindControl("dataid")).Text;
          if (row.RowType == DataControlRowType.DataRow)
          {
          }
      }
  }
  catch (Exception exception) { throw exception; }
  }
  </code></pre>