My application utilizes a dataset with a couple of One to Many relations. If the user adds a new record and adds information to the master part of the form and adds information to the detail part of the form then clicks save. The master record gets saved and the detail throws a “INSERT statement conflicted with the FOREIGN KEY constraint … The conflict occurred in “MasterTable”, “MasterTableID”.
My save routine goes like this:
-
Me.Validate () ‘ Validates the form
-
MasterBindingSource.EndEdit
-
MasterTableTableAdapter.Update(DataSet.MasterTable)
-
DetailBindingsource.EndEdit
-
DetailTableAdapter.Update(DataSet.DetailTable)
-
I checked the row data for the Master Table and it’s identity updated from 0 to the next ID. When I check the foreign column on the Detail table it’s still 0. So when the update hits the constraint is invalid and throws the error. Blaming the Master Table even though it has the updated ID. Is there something wrong in my EndEdit / Update Sequence that the foreign key on the detail table is not updating???