2016-07-10 71 views
-2

StockCalulate AleartC#我想更新SQL不同的值,但錯誤幫助我

圖1:

enter image description here

圖2:

enter image description here

CODE:

try { 

con = new SqlConnection(cs.DBConn); 
con.Open(); 
string cb = "Update OrderDetailNum set ImportPrice = (@d1 /100) * (select StockCalulate=((StockCalulate/Sum(StockCalulate))*100) from OrderDetailNum Where StockID='" + lblStockID.Text + "' AND Orderid='" + comboOrder.Text + "' group by StockCalulate) Where StockID='" + lblStockID.Text + "' AND Orderid='" + comboOrder.Text + "'"; 
cmd = new SqlCommand(cb); 
cmd.Connection = con; 
cmd.Parameters.AddWithValue("@d1", Convert.ToDecimal(lblPr.Text.ToString())); 
cmd.ExecuteReader(); 
con.Close(); 
MessageBox.Show("Successfully updated", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information); 
dataGridView1.DataSource = GetData(); 
ClearAllText(this); 
} catch (Exception ex) { 
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
} 
+0

你似乎更多的是SQL問題,我會編輯的問題,並把只注重查詢。 –

+2

[SQL Server子查詢的可能重複項返回多個值。當子查詢遵循(字符)或子查詢用作表達式時,這是不允許的(http://stackoverflow.com/questions/22909737/sql-server-subquery-returned-more-than-1-value-這是不允許的 - 當 - s) –

+0

[*** SQL注入警報***](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105 %29.aspx) - 您應該**不**將您的SQL語句連接在一起 - 使用**參數化查詢**代替以避免SQL注入 –

回答

-1

嘗試以下

  string cb = string.Format("Update OrderDetailNum set ImportPrice = (@d1 /100) * (select StockCalulate=((StockCalulate/Sum(StockCalulate))*100))" + 
       " from OrderDetailNum" + 
       " Where StockID='{0}' AND Orderid='{1}'", 
       lblStockID.Text, comboOrder.Text); 
+0

EROR索引零基於必須大於或等於零且小於參數列表 –

+0

我刪除了最後一個加號。 – jdweng