可能重复:
Qty Control in C#
我希望mods不介意重新发布这个。下面的代码不能按预期的方式工作。当最终用户点击btQty按钮时,数量变为-2。什么时候应该减少一个(因为销售)。这是代码,并提前感谢..
cs.Open();
int ret;
string Query = "SELECT QTY from tblStock WHERE ID=1";
SqlCommand cmd = new SqlCommand(Query, cs);
string str = Convert.ToString(cmd.ExecuteScalar());
if (str != "")
{
ret = Convert.ToInt32(cmd);
}
else
{
ret = -1;
}
if (ret == 0)
{
lbqty.Text = "Not enough stocks.";
dgUpdate();
}
else
{
cmd.CommandText = "UPDATE tblContacts SET QTY = QTY-1 WHERE ID=1";
cmd.ExecuteNonQuery();
if (ret == 1)
{
lbqty.Text = "Re-order. Remaining stocks: 1";
dgUpdate();
}
else
{
txtQty.Text = "Remaining stocks: " + (ret - 1).ToString();
lbqty.Text = "Remaining stocks: " + (ret - 1).ToString();
dgUpdate();
}
}
cs.Close();