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");
}