2013-05-20 48 views
0

我遇到了一個小問題,讓我稍微踩了一下。我正在使用連接到顯示數據的數據源的gridview。我想要做的是將我想要的行移到另一個表中。我現在擁有的代碼將所有行插入到點擊中,而不僅僅是選定的行。下面是我的按鈕點擊代碼..在gridview中插入一個選定的行到數據庫中ASP.net

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(); 
     } 

我打電話的標籤插入效果很好的數據庫,但它插入每一行,而不僅僅是選擇一個。任何想法都會很棒!謝謝!

+0

請使用參數化查詢! –

回答

0

我終於明白了。我必須放棄我創建的模板中的onclick選擇按鈕,但不是什麼大問題。這是代碼。

protected void gvVehicleImport_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) 
    { 
     SqlConnection conn = new SqlConnection("Data Source=DataBase;Initial Catalog=DataBase;Integrated Security=True"); 
     conn.Open(); 

     string StockNumber; 
     string SalesPerson; 
     string Buyer; 
     string GrossProfit; 
     string DealDate; 
     string Make; 
     string Model; 
     string CarTruck; 
     string NewUsed; 
     string Lender; 
     string AmtFinanced; 
     string RetailLease; 
     string BankName; 
     string Status; 
     string ChangedBy; 

     try 
     { 
      GridViewRow g1 = gvVehicleImport.Rows[e.NewSelectedIndex]; 


      //converts the labels of the gridview into strings 
      StockNumber = (g1.FindControl("lblStockNumber") as Label).Text; 
      SalesPerson = (g1.FindControl("lblSalesPerson") as Label).Text; 
      Buyer = (g1.FindControl("lblBuyer") as Label).Text; 
      GrossProfit = (g1.FindControl("lblGrossProfit") as Label).Text; 
      DealDate = (g1.FindControl("lblDealDate") as Label).Text; 
      Make = (g1.FindControl("lblMake") as Label).Text; 
      Model = (g1.FindControl("lblModel") as Label).Text; 
      CarTruck = (g1.FindControl("lblCarTruck") as Label).Text; 
      NewUsed = (g1.FindControl("lblNewUsed") as Label).Text; 
      Lender = (g1.FindControl("lblLender") as Label).Text; 
      AmtFinanced = (g1.FindControl("lblAmtFinanced") as Label).Text; 
      RetailLease = (g1.FindControl("lblRetailLease") as Label).Text; 
      BankName = (g1.FindControl("lblBankName") as Label).Text; 
      Status = (g1.FindControl("lblStatus") as Label).Text; 
      ChangedBy = (g1.FindControl("lblChangedBy") as Label).Text; 


      //inserts statement inserts above strings into table 
      SqlCommand addImport = new SqlCommand("INSERT INTO Vehicle(v_StockNumber, v_SalesPerson, v_Buyer, v_GrossProfit, v_DealDate, v_Make, v_Model, v_CarTruck, v_NewUsed, v_Lender, v_AmtFinanced, v_RetailLease, v_BankName, v_Status, v_ChangedBy)" 
      + "VALUES(@v_StockNumber, @v_SalesPerson, @v_Buyer, @v_GrossProfit, @v_DealDate, @v_Make, @v_Model, @v_CarTruck, @v_NewUsed, @v_Lender, @v_AmtFinanced, @v_RetailLease, @v_BankName, @v_Status, @v_ChangedBy)", conn); 
      addImport.Parameters.AddWithValue("@v_StockNumber", StockNumber); 
      addImport.Parameters.AddWithValue("@v_SalesPerson", SalesPerson); 
      addImport.Parameters.AddWithValue("@v_Buyer", Buyer); 
      addImport.Parameters.AddWithValue("@v_GrossProfit", GrossProfit); 
      addImport.Parameters.AddWithValue("@v_DealDate", DealDate); 
      addImport.Parameters.AddWithValue("@v_Make", Make); 
      addImport.Parameters.AddWithValue("@v_Model", Model); 
      addImport.Parameters.AddWithValue("@v_CarTruck", CarTruck); 
      addImport.Parameters.AddWithValue("@v_NewUsed", NewUsed); 
      addImport.Parameters.AddWithValue("@v_Lender", Lender); 
      addImport.Parameters.AddWithValue("@v_AmtFinanced", AmtFinanced); 
      addImport.Parameters.AddWithValue("@v_RetailLease", RetailLease); 
      addImport.Parameters.AddWithValue("@v_BankName", BankName); 
      addImport.Parameters.AddWithValue("@v_Status", Status); 
      addImport.Parameters.AddWithValue("@v_ChangedBy", ChangedBy); 

      //executes the import 
      addImport.ExecuteNonQuery(); 
      //} 

      //closes connection 
      conn.Close(); 
      lblImportMessage.Text = "Row move successful."; 


     } 
     catch (Exception ex) 
     { 
      lblImportMessage.Text = "Row move was unsuccessful, " + ex.ToString(); 
     } 
    } 

我已經試過許多東西,許多得到它插入單行沒有數據,但是這一次似乎是正確的現在的工作沒有問題啊。希望這有助於任何有困難的人。 再次感謝!

+0

不要忘記將SelectedIndexChanged更改爲ASP中的SelectedIndexChanging! – Humpy

2

你可以使用在GridView
SelectedRow屬性中刪除循環和使用這樣的事情

GridViewRow g1 = gvVehicleImport.SelectedRow; 
if(g1 != null) 
{ 
    ... // code to insert 
} 

說,請儘快刪除該字符串連接是建立你的SQL命令,並使用parameterized query。這將允許你避免語法錯誤(當你的輸入文本包含單引號時)和Sql Injection,其中惡意用戶試圖破壞你的分區

+0

太棒了。我想我曾嘗試過這種變化,但我一定會在早上再試一次!謝謝您的幫助!如果我能讓它工作,我會告訴你的! – Humpy

+0

我已經嘗試了這一次,它仍然無法正常工作。沒有行被添加到表格,儘管我的標籤聲稱它已正確移動。我確實回去並參數化查詢。再次感謝那個想法。如果您有任何其他想法,請告訴我!再次感謝! – Humpy

+0

好吧,上面的代碼在試圖使用自己的代碼插入數據之前,如果SelectedRow不爲null,則只會添加一個檢查。所以唯一可能的解釋是當您調用該代碼時SelectedRow爲空。 – Steve

相關問題