title: C# asp.net - Update specific row on SQL Server
body: <p>I currently have an asp.net gridview with a checkbox column and button. I am trying to update the database table where the checkbox has been checked. </p>
  
  <p>My SQL table has the following fields </p>
  
  <blockquote>
    <p>row_id(int)<br>
    MagazineCode(varchar)<br>
    Dimensions(varchar)<br>
    Obselete(int)</p>
  </blockquote>
  
  <p>WEBFORM.aspx</p>
  
  <pre><code> &lt;div id="effortGridHolder" style="width: 888px; height: 270px; overflow: auto;"&gt;
          &lt;asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1"
              AutoGenerateColumns="False" AlternatingRowStyle-CssClass="alt"
              OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CellPadding="4"
              ForeColor="#333333" GridLines="None" Width="100%"
              DataKeyNames="MagazineCode" &gt;
  
              &lt;Columns&gt;
  
                  &lt;asp:BoundField DataField="MagazineCode"
                      HeaderText="MagazineCode" SortExpression="MagazineCode" /&gt;
  
                  &lt;asp:BoundField DataField="Profile_ID"
                      HeaderText="Profile_ID" SortExpression="Profile_ID" /&gt;
  
                  &lt;asp:BoundField DataField="Dimensions"
                      HeaderText="Dimensions" SortExpression="Dimensions" /&gt;
  
      &lt;asp:templatefield HeaderText="Obselete"&gt;
      &lt;itemtemplate&gt;
      &lt;asp:checkbox ID="cbSelect"    runat="server"&gt;&lt;/asp:checkbox&gt;
      &lt;/itemtemplate&gt;
      &lt;/asp:templatefield&gt;
              &lt;/Columns&gt; 
  
  &lt;asp:Button ID="obselete_btn" OnClick="obselete_submit" runat="server" Text="Update" /&gt;
  </code></pre>
  
  <p>The C# code</p>
  
  <pre><code>protected void obselete_submit(object sender, EventArgs e)
  {
      //GET CHECKED VALUES
      foreach(GridViewRow row in GridView1.Rows)
      {
          if(row.RowType == DataControlRowType.DataRow)
          {
              CheckBox chkRow = (row.Cells[2].FindControl("cbSelect") as CheckBox);
              if (chkRow.Checked)
              {
                  //update 
                  SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationData"].ConnectionString);
                  SqlCommand cmd = conn.CreateCommand();
                  cmd.CommandText = "UPDATE table SET Obselete = '1' WHERE MagazineCode = '" + tbMagCode_GAD.Value + "'";
                  conn.Open();
                  cmd.ExecuteNonQuery();
                  conn.Close();
              }
          }
      }
  }
  </code></pre>
  
  <p>At the moment I am able to get the checked values from checkboxes and when the update button is pressed, it is able to update the Obselete and set it to 1. </p>
  
  <p>The current SQL query for this is:</p>
  
  <pre><code>"UPDATE table SET Obselete = '1' WHERE MagazineCode = '" + tbMagCode_GAD.Value + "'";
  </code></pre>
  
  <p>But this isnt great as the database table can have the same MagazineCode and will update the Obselete field for all of them.  I have created a new field called <code>row_id</code> and want update the table where <code>row_id = "gridview row clicked"</code> </p>
  
  <p>Thanks </p>