简体   繁体   中英

Data update problem in SQL from DataGridView in C#

I have two tables in SQL server and I have a form purchaseinvoiceform . it uses a DataGridView to save multiple products.

In save mode this works well. But when I update any order it's updating only the first table which is purchaseinvoice , but isn't updating the second table purchaseinvoicedetails . purchaseinvoicedetails saves multiple products by their ordernumber . If I have one item in datagridview it saves successfully. Now when I update this one item, no problem occurs. But when I add one more item in this order and try to update it, it does not save the other items.

Update query:

SaveOrUpdateProductDetailsOnly("UPDATE [dbo].[PurchaseInvoiceDetails] SET [ProductCode] = @ProductCode ,[ProductName] = @ProductName ,[Box] = @Box,[Quantity] = @Quantity ,[Price] = @Price,[DiscountInPercent] = @DiscountInPercent ,[DiscountAmount] = @DiscountAmount,[Amount] = @Amount WHERE PurchaseInvoiceNo = @PurchaseInvoiceNo");

Here is the Code

How about this?

private void btnUpdate_Click(object sender, EventArgs e)
{

    using (SqlConnection con = new SqlConnection("Server=your_server_name;Database=your_database_name;Trusted_Connection=True;"))
    {

        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Courses", con))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                {
                    SqlCommandBuilder sqlcmd = new SqlCommandBuilder(da);
                    DataSet ds = new System.Data.DataSet(); // remove this line
                    da.Update(this.ds, "Courses");
                }
            }
        }
    }
}

我认为如果您想更新第二个表中的所有记录,您需要将所有 cmd.parameters 放在 foreach 语句中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM