0

I am currently working on a Windows Forms application using C#. I have two tables (tblBatch & tblProduct). These tables are related to each other based on the BatchID.

i.e

-> tblBatch
    BatchID
    BatchName

And

-> tblProduct
    ProductID
    ProductName
    BatchID

Now there are two different situations.

  1. Product registration (adding a specific item to the database - stalk quantity could be empty because this will just register a product.
  2. Supplying stalk quantity, updating the quantity amount of the products and assigning the batchNo to products.

At this point, the batchID must already be present in the database before I would assign it to a product in tblProduct. Because, product batch cannot be assigned before supply of products, and as during this stage, I don't have any supply which can give me an idea of the current batchNo. So during product registration, I just want to assign null to the BatchID in tblProduct. Once a supply will be performed later, I will update this ID and will assign the exact BatchID to the supplied Items.

However, I am getting the following error even I have tried keeping the BatchID field null in tblProduct but still I am unable to add record into the database.

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_tblProduct_tblBatch". The conflict occurred in database "myDB", table "dbo.tblBatch", column 'BatchID'.

Is there any solution to this problem? Or someone suggest me a simple way to resolve this specific problem.

Noor
  • 185
  • 2
  • 18
  • Your logic is correct, you would want to assign `null` to `tblProduct:BatchId`. Why that is not working I (and anyone else reading this) don't know because there is no code in your question. If you want help please include the code where you do the assignment and persisting of the new record. Better yet, include an [mcve]. – Igor Jan 30 '19 at 22:02
  • Check the below link. https://stackoverflow.com/questions/2366854/can-table-columns-with-a-foreign-key-be-null – Mano Feb 01 '19 at 05:06

0 Answers0