[英]How to Create a Sell Button Code In Visual Studio C# with SQL Server as database
I'm using Visual Studio C# and I'm having a conflict on how to create a sell button as well how can I get the total amount of product that had been sold here is my code我正在使用 Visual Studio C# 并且我在如何创建销售按钮以及如何获得已售出的产品总量方面存在冲突是我的代码
try
{
if (txtID.Text != "" && txtCategory.Text != "" &&
txtName.Text != "" && txtQty.Text != "" &&
txtQty.Text != "" && PriceText.Text != "" &&
SuppNameText.Text != "")
{
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO Sold_Inventory
(ProductName, Description, Category, Quantity_Sold, Price,
TotalAmount, SuppliersName)
VALUES(@ProductName, @Description, @Category, @Quantity, @Price,
@Supplier)
WHERE TotalAmount = @Quantity * @Price", con);
cmd.Parameters.AddWithValue("@ProductName", txtID.Text);
cmd.Parameters.AddWithValue("@Description", txtName.Text);
cmd.Parameters.AddWithValue("@Category", txtCategory.Text);
cmd.Parameters.AddWithValue("@Quantity", txtQty.Text);
cmd.Parameters.AddWithValue("@Price", PriceText.Text);
cmd.Parameters.AddWithValue("@Supplier", SuppNameText.Text);
cmd.ExecuteNonQuery();
SqlCommand cmd1 = new SqlCommand(@"UPDATE Quantity
FROM Inventory SET Quantity = Quantity - @Quantity");
cmd1.Parameters.AddWithValue("@Quantity", txtQty.Text);
cmd1.ExecuteNonQuery();
con.Close();
ClearTextbox();
btnSearch.Enabled = true;
txtSearch.Enabled = true;
groupBox4.Enabled = true;
btnSubmit.Enabled = false;
btnCancel.Enabled = false;
ClearTextbox();
DisabledTextbox();
btnAdd.Enabled = true;
RefreshDGV(this.dataGridView1);
}
else
{
MessageBox.Show("You left an empty field!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
The syntax of the first SqlCommand is wrong.第一个 SqlCommand 的语法是错误的。
INSERT INTO doesn't require a WHERE clause and you are missing one parameter placeholder in your query text. INSERT INTO 不需要 WHERE 子句,并且您的查询文本中缺少一个参数占位符。
So the first fix is around your command text所以第一个修复是围绕你的命令文本
SqlCommand cmd = new SqlCommand(@"INSERT INTO Sold_Inventory
(ProductName,Description,Category,Quantity_Sold,Price,
TotalAmount,SuppliersName)
VALUES(@ProductName,@Description,@Category,@Quantity,@Price,
@TotalAmount, @Supplier)", con);
Now calculate the value for the @TotalAmount parameter in code like this....现在在这样的代码中计算@TotalAmount 参数的值......
decimal total = Convert.ToDecimal(txtQty.Text) *
Convert.ToDecimal(PriceText.Text);
cmd.Parameters.AddWithValue("@TotalAmount", total);
Finally, the command that decrements the quantity should identify the exact record for the product that you have sold.最后,减少数量的命令应该标识您所售产品的准确记录。 Here the WHERE clause is fundamental.
这里 WHERE 子句是基本的。
I suppose that your Product table has a record for the product that you have sold identified by something like a ProductID primary key.我想你的 Product 表有一个你所销售的产品的记录,由 ProductID 主键之类的东西标识。 (Also you need to add the connection when you build the command)
(另外你需要在构建命令时添加连接)
SqlCommand cmd1 = new SqlCommand(@"Update Inventory
SET Quantity = Quantity - @Quantity
WHERE ProductID = @productID", con);
There are other considerations to your code.您的代码还有其他注意事项。 First, forget the AddWithValue method.
首先,忘记 AddWithValue 方法。 It is an easy shortcut, but it can create a lot of problems if you pass a string as parameter value and the column that receives the value is not a text field (the automatic conversion from string to decimal could fail because the localization of your client code doesn't match the rules used by the database engine)
这是一个简单的快捷方式,但是如果您将字符串作为参数值传递并且接收该值的列不是文本字段(从字符串到十进制的自动转换可能会失败,因为您的客户端的本地化),它会产生很多问题代码与数据库引擎使用的规则不匹配)
Better use always the Add method specifying the datatype of the parameter.最好始终使用指定参数数据类型的 Add 方法。
For example, the Price column probably wants a decimal not a string, so例如,价格列可能需要小数而不是字符串,所以
decimal price = Convert.ToDecimal(PriceText.Text);
cmd.Parameters.Add("@Price", SqlDbType.Decimal).Value = price;
Of course the same applies to all other values.当然,这同样适用于所有其他值。 Don't let AddWithValue figure out what do you mean with your parameter.
不要让 AddWithValue 弄清楚你的参数是什么意思。 Be explicit.
明确一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.