title: Checkbox asp.net c# Grid
body: <p>I currently have a Data Grid that </p>
  
  <pre><code>"SELECT * FROM table WHERE Hentet = 'nej';"
  </code></pre>
  
  <p>under that grid I have a exact copy of it, but </p>
  
  <pre><code>"WHERE Hentet = 'Ja';"
  </code></pre>
  
  <p>as you will see in the code.</p>
  
  <p>Both grid's work fine and get the data they are supposed to get, however there is a check box on each row of "Grid 1", so you can Select Whatever Rows, Press the button and I want the value <code>Nej;</code> to change to <code>Ja;</code> on that button Click, so the rows with a checked check box will be moved to Grid2, now that their value is = <code>Ja</code>. Grid2 does not have a check box column, so its only from grid1 to grid2, and not both ways.</p>
  
  <p><strong>Grid 1</strong></p>
  
  <pre><code>    &lt;asp:DataGrid ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false"&gt;
             &lt;Columns&gt;
             &lt;asp:TemplateColumn&gt;
                 &lt;ItemTemplate&gt;
                 &lt;asp:CheckBox ID="CheckBox1" runat="server"&gt;&lt;/asp:CheckBox&gt;
                     &lt;/ItemTemplate&gt;
             &lt;/asp:TemplateColumn&gt;
  
             &lt;asp:BoundColumn DataField="dato" HeaderText="Dato"&gt;
              &lt;/asp:BoundColumn&gt;
              &lt;asp:BoundColumn DataField="Antal" HeaderText="Antal"&gt;
              &lt;/asp:BoundColumn&gt;
             &lt;asp:Boundcolumn HeaderText="Navn" Datafield="VareNAvn"&gt;
              &lt;/asp:Boundcolumn&gt;
         &lt;asp:BoundColumn DataField="KøbtAfBrugerID" HeaderText="Købt af ID"&gt;
              &lt;/asp:BoundColumn&gt;
              &lt;asp:Boundcolumn HeaderText="Hented" DataField="Afhented"&gt;
                  &lt;/asp:Boundcolumn&gt;
         &lt;/Columns&gt;
      &lt;/asp:DataGrid&gt;
    &lt;asp:Button ID="Button_Hented" CssClass="btfarve" runat="server" Text="Afhentet" OnClick="Button_Hented_Click" /&gt;
      &lt;asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="&lt;%$ ConnectionStrings:DatabaseConnectionString1 %&gt;" 
      SelectCommand="SELECT * FROM [Transactioner] WHERE afhented ='Nej';"&gt;
      &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p><strong>Grid2</strong></p>
  
  <pre><code>&lt;asp:DataGrid ID="GridView2" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="false"&gt;
  
             &lt;Columns&gt;
                 &lt;asp:BoundColumn DataField="dato" HeaderText="Dato"&gt;
                  &lt;/asp:BoundColumn&gt;
                  &lt;asp:BoundColumn DataField="Antal" HeaderText="Antal"&gt;
                  &lt;/asp:BoundColumn&gt;
                 &lt;asp:Boundcolumn HeaderText="Navn" Datafield="VareNAvn"&gt;
                  &lt;/asp:Boundcolumn&gt;
              &lt;asp:BoundColumn DataField="KøbtAfBrugerID" HeaderText="Købt af ID"&gt;
                  &lt;/asp:BoundColumn&gt;
                  &lt;asp:Boundcolumn HeaderText="Hented" DataField="Afhented"&gt;
                      &lt;/asp:Boundcolumn&gt;
             &lt;/Columns&gt;
          &lt;/asp:DataGrid&gt;
          &lt;asp:SqlDataSource ID="SqlDataSource2" runat="server" 
          ConnectionString="&lt;%$ ConnectionStrings:DatabaseConnectionString1 %&gt;" 
          SelectCommand="SELECT * FROM [Transactioner] WHERE afhented ='Ja';"&gt;
          &lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p><strong>Code Behind</strong></p>
  
  <pre><code>protected void Button_Hented_Click(object sender, EventArgs e)
       {
           string Hejsa;
  
           SqlConnection conn = new SqlConnection();
           conn.ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
  
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = conn;
           cmd.CommandText = "SELECT * FROM Transactioner";
  
  
  
           conn.Open();
           SqlDataReader reader = cmd.ExecuteReader();
  
           if (reader.Read())
           {
  
               Hejsa = reader["Id"].ToString();
  
  
  
               foreach (DataGridItem item in GridView1.Items)
               {
                   CheckBox Cb = item.Cells[0].Controls[1] as CheckBox;
  
  
                   if (Cb.Checked)
                   {
  
                       SqlConnection conn2 = new SqlConnection();
                       conn2.ConnectionString =
                           ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
                       SqlCommand cmd2 = new SqlCommand();
                       cmd2.Connection = conn2;
  
                       cmd2.CommandText = "UPDATE Transactioner"
                           + " SET Afhented = @Afhented"
                           + " where Id = @Id";
  
  
                       cmd2.Parameters.Add("@Afhented", SqlDbType.NVarChar).Value = "Ja";
                       cmd2.Parameters.Add("@Id", SqlDbType.Int).Value = Hejsa;
  
  
                       conn2.Open();
                       cmd2.ExecuteNonQuery();
                       conn2.Close();
  
                   }
  
               }
               Response.Redirect(Request.RawUrl);
  
           }
           conn.Close();
  
          }
  </code></pre>
  
  <p>So this is all the code, I get NO error messages atall and i really cant see what is wrong again, All i really want the code to do is on button click, if checkbox is checked, change the value 'Nej' to 'Ja'.
  Sorry if my question was hard to understand.
  Tekar</p>