减去两个数据集值并将其保存在新列中

Subtract two dataset values and save it in a new column

根据数据集值我有一个小问题,我想减去这些值并将其保存为一个新的变量列,这是我的代码:

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

错误是:

Operator '-' cannot be applied to operands of type 'string' and 'object'

我假设,数量是一个整数值。 您必须将值解析为整数才能用它们计算

替换

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  + "'";
StrSQL = StrSQL + " Balance = '" + (Convert.ToDecimal(rs.Tables[0].Rows[i]["RQty"])) - (Convert.ToDecimal(rs.Tables[0].Rows[i]["IQty"])) + "'";