0

I am trying to insert some values to SalesProductTable and SalesMaintable which are in my database, but when I click the save button, the insert occurrs successfully but it give the following error:

The INSERT statement conflicted with the FOREIGN KEY constraint"FK_SalesProductTable_SalesMainTable". The conflict occurred in database "IndusPharmaManagement", table "dbo.SalesMainTable", column 'SalesID'.

The code which share the following two methods is:

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

Here is my code:

    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");
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
thenativeguy
  • 25
  • 1
  • 8
  • 4
    the error msg is very specific....Also, SQL injection.... – Mitch Wheat Oct 24 '18 at 03:47
  • Among these two tables, which is the table with primary key and which is using it as foreign key? Which method you are calling first and which one next? Can you also share the code which calls these methods ? Which method gives the error/ – Chetan Oct 24 '18 at 03:50
  • 1
    Possible duplicate of [INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server](https://stackoverflow.com/questions/2965837/insert-statement-conflicted-with-the-foreign-key-constraint-sql-server) – Tetsuya Yamamoto Oct 24 '18 at 03:51
  • SalesMainTable has the primary key SalesID while this SalesID is the foreign key in the SalesProductTable – thenativeguy Oct 24 '18 at 03:52
  • `MainSaleInsertion` method adds new row to `SalesMainTable` table but the `SalesID` of the new row is not available in `individualSaleEntry` method. That's why you are getting this error. You are trying to use `CashSalesDgv.Rows[i].Cells[1].Value` to populate `SalesID` in `individualSaleEntry` method which might be a not a valid value. So you need to make sure that you have the new SalesID available in `individualSaleEntry` method. – Chetan Oct 24 '18 at 04:03

0 Answers0