I have 2 tables, an Owners
table with MemberID
as the primary key, and a Tigers
table with TigerID
as primary key and MemberID
as a foreign key to indicate who owns the tiger. Naturally, an owner can own multiple tigers.
I'm displaying the tigers in a DataGridView using a DataAdapter in a Windows Form (Visual Studio let's you drag and drop a table onto a windows form and auto generates the code for you).
When I run the form, I get an error:
System.Data.ConstraintException: 'Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.'
EDIT 2:
I tried removing data again to see if having unique foreign keys erases the issue. It doesn't. I apologise for earlier wrong information. I also checked and there is no foreign key MemberID
in Tigers
which is not a primary key in Owners
EDIT:
CREATE TABLE [dbo].[Members] (
[MemberID] INT IDENTITY (1, 1) NOT NULL,
[FName] VARCHAR (50) NOT NULL,
[LName] VARCHAR (50) NOT NULL,
[Street] VARCHAR (MAX) NOT NULL,
[HouseNbr] INT NOT NULL,
[PostNbr] INT NOT NULL,
[City] NCHAR (10) NOT NULL,
PRIMARY KEY CLUSTERED ([MemberID] ASC)
);
CREATE TABLE [dbo].[Tigers] (
[TigerID] INT IDENTITY (1, 1) NOT NULL,
[Name] VARCHAR (50) NOT NULL,
[Type] VARCHAR (50) NOT NULL,
[Weight] INT NOT NULL,
[MemberID] INT NOT NULL,
PRIMARY KEY CLUSTERED ([TigerID] ASC)
);
EDIT 3 Code in the Tigers Form
private void tigersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.tigersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.tRFDDataSet);
}
private void frmTigerList_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'tRFDDataSet.Tigers' table. You can move, or remove it, as needed.
this.tigersTableAdapter.Fill(this.tRFDDataSet.Tigers);
}
EDIT 4:
I went to the definition of the Tiger DataGrid Fill
and Found this code:
private TRFDDataSet tRFDDataSet;
private System.Windows.Forms.BindingSource tigersBindingSource;
private TRFDDataSetTableAdapters.TigersTableAdapter tigersTableAdapter;
private TRFDDataSetTableAdapters.TableAdapterManager tableAdapterManager;
private System.Windows.Forms.DataGridView tigersDataGridView;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn4;
private System.Windows.Forms.DataGridViewTextBoxColumn dataGridViewTextBoxColumn5;
So I added before the tigersBindingSource
private System.Windows.Forms.BindingSource membersBindingSource;
private TRFDDataSetTableAdapters.MembersTableAdapter membersTableAdapter;
But it still didn't work. The error persisted
EDIT FINAL:
Dragging and dropping the Owners table worked and setting the generated code in the Form Designer code BEFORE the Tiger table code is the solution. This is due to the correct reply's insight about the parent table needing to load first in the same instance. I only need to find a way to edit that such I can hide the Owners table from displaying in this form.