INSERT语句与FOREIGN KEY约束“ FK_SalesProductTable_SalesMainTable” 1发生冲突

时间:2018-10-24 03:46:11

标签: c# sql-server

我正在尝试向数据库中的SalesProductTable和SalesMaintable插入一些值,但是当我单击“保存”按钮时,插入成功,但是出现以下错误:

  

INSERT语句与FOREIGN KEY约束“ FK_SalesProductTable_SalesMainTable”冲突。在数据库“ IndusPharmaManagement”的表“ dbo.SalesMainTable”的“ SalesID”列中发生了冲突。

共享以下两种方法的代码是:

 private void btnSave_Click(object sender, EventArgs e)
 {
     MainSaleInsertion();
     individualSaleEntry();
 }

这是我的代码:

    public void individualSaleEntry()
    {
        try
        {
            for (int i = 0; i < CashSalesDgv.Rows.Count; i++)
            {
                string InsertSaleProductEntry = @"INSERT INTO SalesProductTable (SalesID,ItemName,Price,Qty,TotalAmount,Date) VALUES ('" + CashSalesDgv.Rows[i].Cells[1].Value + "','" + CashSalesDgv.Rows[i].Cells[3].Value + "','" + CashSalesDgv.Rows[i].Cells[4].Value + "','" + CashSalesDgv.Rows[i].Cells[5].Value + "','" + CashSalesDgv.Rows[i].Cells[6].Value + "','" + dateTimeSale.Text + "')";
                objDAC.ExecQuery(InsertSaleProductEntry);
                objDAC.infrmUser("Data saved successfully in Product Sales", "Information");
            }
        }
        catch (Exception e)
        {

        }
    }

    public void MainSaleInsertion()
    {
        string InsertSalesMainEntry = @"INSERT INTO SalesMainTable (SalesID,NoOfItems,TotalQty,DiscountPer,DiscountRs, TotalToPay,Paid,Dues,Date) VALUES
                                         ('" + txtInvoiceNo.Text.Trim() + "','" + txtNoOfItems.Text.Trim() + "','" + txtTotalQty.Text.Trim() + "','" + txtDiscountPercentage.Text.Trim() + "','" + txtDiscountRs.Text.Trim() + "','" + txtTotalPayment.Text.Trim() + "','" + txtTotalPayment.Text.Trim() + "',null,'" + dateTimeSale.Text + "')";
        objDAC.ExecQuery(InsertSalesMainEntry);
        objDAC.infrmUser("Data saved successfully in Main Sales", "Information");
    }

0 个答案:

没有答案