Loading

Add new comment

Submitted by: 


If you encounter the following error:

System.Data.InvalidConstraintException: ForeignKeyConstraint FK_foreign_key_name
requires the child key values (-1) to exist in the parent table.

Or

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_foreign_key_name ". The conflict occurred in database "YourDatabase", table "dbo.Tablename", column 'YourPrimaryKey'. The statement has been terminated.

Probably you are using a Parent-Child table or Master-Detail table. This error occurs if you have a parent-child form with a Details form and a DataGridView control.

The errors above will pop-up after you try to save your record. The solution to this is to issue an EndEdit command before you try to edit your child/DatagridView control.

You can do this, for example, in the GotFocus event of your DataGridView control like:

  1. Private Sub ProductDetailsDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ProductDetailsDataGridView.GotFocus
  2. Me.ProductsBindingSource.EndEdit()
  3. End Sub



Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.