title: Inserting a single selected row in gridview into database ASP.net
body: <p>I have run into a little problem that has stomped me a little bit. I am using a gridview that is connected to a datasource that displays the data. What I am trying to do is move a row that I would like into another table. The code I currently have now, inserts all rows on the click instead of just the selcted row. Here is the code for my button click.. </p>
  
  <pre><code>    protected void lbSelect_OnClick(object sender, EventArgs e)
      {
  
          conn.Open();
          tran = conn.BeginTransaction();
          cmd.Transaction = tran;
          //string slno = null;
  
          try
          {
              foreach (GridViewRow g1 in gvVehicleImport.Rows)
              {
                  string StockNumber = (g1.FindControl("lblStockNumber") as Label).Text;
                  string SalesPerson = (g1.FindControl("lblSalesPerson") as Label).Text;
                  string Buyer = (g1.FindControl("lblBuyer") as Label).Text;
                  string GrossProfit = (g1.FindControl("lblGrossProfit") as Label).Text;
                  string DealDate = (g1.FindControl("lblDealDate") as Label).Text;
                  string Make = (g1.FindControl("lblMake") as Label).Text;
                  string Model = (g1.FindControl("lblModel") as Label).Text;
                  string CarTruck = (g1.FindControl("lblCarTruck") as Label).Text;
                  string NewUsed = (g1.FindControl("lblNewUsed") as Label).Text;
                  string Lender = (g1.FindControl("lblLender") as Label).Text;
                  string AmtFinanced = (g1.FindControl("lblAmtFinanced") as Label).Text;
                  string RetailLease = (g1.FindControl("lblRetailLease") as Label).Text;
                  string BankName = (g1.FindControl("lblBankName") as Label).Text;
                  string Status = (g1.FindControl("lblStatus") as Label).Text;
                  string ChangedBy = (g1.FindControl("lblChangedBy") as Label).Text;
  
                  string query = "INSERT INTO Vehicle VALUES('" + StockNumber + "','" + SalesPerson + "','" + Buyer + "','" + GrossProfit + "','" + DealDate + "','" + Make + "','" + Model + "','" + CarTruck + "','" + NewUsed + "','" + Lender + "','" + AmtFinanced + "','" + RetailLease + "','" + BankName + "','" + Status + "','" + ChangedBy + "')";
                  //slno = StockNumber;
                  cmd.CommandText = query;
                  cmd.ExecuteNonQuery();
              }
  
  
              tran.Commit();
              conn.Close();
              lblImportMessage.Text = "Row move successful.";
  
  
          }
          catch (Exception ex)
          {
              tran.Rollback();
              lblImportMessage.Text = "Row move was unsuccessful, " + ex.ToString();
          }
  </code></pre>
  
  <p>I am calling the labels to insert into the database which works well, but it inserts every row, not just the selected one. Any thoughts would be great! Thanks!</p>