2014-04-26 44 views
-1

我想更新我的庫存數量,保持以前的庫存狀態。我需要根據內部或外部交易增加或減少以前的數據。我沒有獲得當前庫存更新的股票。 //這是問題段的開始我想更新我的庫存數量,保持以前的數量。

 SqlCommand cmd2 = new SqlCommand("select stock from currentstock where [email protected]", con); 
     con.Open(); 
     int x=0; 
     cmd2.Parameters.AddWithValue("@a",comboBox2.SelectedItem.ToString()); 
     SqlDataReader dr = cmd2.ExecuteReader(); 
     if (dr.Read()) 
     { 
      x = dr.GetInt16(2); 
     } 
     if (radioButton1.Checked == true) 
     { 
      x = x + Convert.ToInt16(textBox1.Text); 
     } 
     else if (radioButton2.Checked == true) 
     { 
      x = x - Convert.ToInt16(textBox2.Text); 
     } 
      con.Close(); 



     SqlCommand cmd1 = new SqlCommand("update currentstock set [email protected] where [email protected]",con); 
     con.Open(); 
     cmd1.Parameters.AddWithValue("@a", x); 
     cmd1.Parameters.AddWithValue("@b", comboBox2.SelectedItem.ToString()); 
     cmd1.ExecuteNonQuery(); 
     con.Close(); 

//這是問題段結束

+0

那麼問題出現在哪裏? –

+0

x = x + Convert.ToInt16(textBox1.Text); x = x - Convert.ToInt16(textBox2.Text); 我認爲這兩個陳述沒有得到執行。 – user3275519

+0

請解釋你在哪條線上面臨的問題以及出現什麼樣的錯誤或異常, –

回答

0

首先請說你的SQL表時從用戶輸入不直接使用價值(見SQL Injection) 。使用存儲過程。

我期望您從組合框返回的值不是表的itemname列中的有效值。

+0

這是一個有效的值,調試程序沒有顯示任何錯誤。組合框將返回一個字符串格式的值,並使用項目名稱作爲SQL表中的nvarchar類型。 – user3275519