2015-04-02 37 views
0

我不得不根據數據的設定值,我想減去這些值並保存新的變量列一個小問題,這是我的代碼:提取兩個數據集的值,並將其保存在一個新的列

StrSQL = "Select * from Stocks"; 
rs = (DataSet) MethodClass.ConnectionToQuery(StrSQL); 
for (i = 0; i < rs.Tables[0].Rows.Count; i++) 
{ 
StrSQL = " Update Stocks Set "; 

// Error is in below line 
StrSQL = StrSQL + " Balance = '" + (rs.Tables[0].Rows[i]["RQty"]) - (rs.Tables[0].Rows[i]["IQty"]) + "'"; 

StrSQL = StrSQL + " Where ProductCode = '" + rs.Tables[0].Rows[i]["ProductCode"] + "'"; 
MethodClass.ConnectionToQueryCommand(StrSQL, "ExecuteNonQuery"); 
} 

錯誤是:

運算符「 - 」不能被應用於類型「字符串」和「對象」

+0

你讀過您的錯誤信息?正如它所說的,你不能在'string'和'object'之間使用'-'運算符。兩個'rs.Tables [0] .Rows [I] [ 「RQty」]'和'rs.Tables [0] .Rows [I] [ 「IQty」]''返回object'。 – 2015-04-02 06:23:24

+0

是的,我讀了,但系統將接受第一個值作爲一個字符串 – MaroofQaiser 2015-04-02 06:30:47

回答

0

我假定的操作數,該數量是一個整數值。 你必須解析值作爲整數能夠與它們來計算

更換

StrSQL = StrSQL + " Balance = '" + (rs.Tables[0].Rows[i]["RQty"]) - (rs.Tables[0].Rows[i]["IQty"]) + "'"; 

int Value1 = int.Parse(rs.Tables[0].Rows[i]["RQty"]); 
int Value2 = int.Parse(rs.Tables[0].Rows[i]["IQty"]); 
int Result = Value1 - Value2 ; 
StrSQL = StrSQL + " Balance = '" + Result + "'"; 
+0

是的,它可以完美運行。 – MaroofQaiser 2015-04-03 10:52:52

0
StrSQL = StrSQL + " Balance = '" + (Convert.ToDecimal(rs.Tables[0].Rows[i]["RQty"])) - (Convert.ToDecimal(rs.Tables[0].Rows[i]["IQty"])) + "'"; 
+0

'decimal.Parse'? – 2015-04-02 06:21:13

+0

@SonerGönül理想的數量應該是十進制值。 – prashant 2015-04-02 06:23:09

+0

所有值都是整數 – MaroofQaiser 2015-04-02 06:27:29

相關問題