0

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.

wajd
  • 13
  • 7
  • Were you able to initialize your tables in SQL? – David Lee Jun 06 '17 at 20:54
  • Start by showing the table and constraint definitions, if possible. – mjw Jun 06 '17 at 20:56
  • @mjw How do i access the constraint definitions? I used the graphic interface in the xsd file to create the relation. It's set with Owners as a parent child to Tigers with the relation as 'relation and foreign key constraint' – wajd Jun 06 '17 at 21:13
  • System.Data.ConstraintException is thrown by DataSet/DataTable objects you also need to post your C# code that creates DataTables fills them with data – user6144226 Jun 06 '17 at 21:15
  • @DavidLee What do you mean by that? I apologize for my clueless-ness, I've used the graphic interfaces so far. (I'm still learning) Edit: Maybe you mean if i was able to have tables? The answer is yes, since I have data in them and can display the Owners table just fine in a different Windows Form – wajd Jun 06 '17 at 21:15
  • https://stackoverflow.com/a/7029713/2333499 Manually add a Try/Catch block to the generated code like so and then breaking when the exception is raised to see the error detail. – SqlZim Jun 06 '17 at 21:26
  • @user6144226 Where do i Find that code? – wajd Jun 06 '17 at 21:33
  • @SqlZim I tried adding a Try Catch around the tigersTableAdapter.Fill but instead of raising and exception, it showed the table but with red exclamations at the begining of each table – wajd Jun 06 '17 at 21:41
  • @WajdTohme No worries, I was just wondering if your tables show up in SQL Server. I was thinking you were doing a code first database, but from your edits I can see that this is not the case. I was just trying to figure out if this was an issue when creating your database or when you were querying against it. – David Lee Jun 06 '17 at 21:53
  • @DavidLee Yeah no. I'm pretty much winging it. I set a goal to make an app with certain functions and I'm learning as I go. The course book I have is absolute shit since it only shows how to add one table using the graphic interface. There doesn't seem to be online sources available either other than documentation which is hard to swallow when you're new. – wajd Jun 06 '17 at 21:58
  • @WajdTohme the only point of learning how to use TypedDataSets is to find out how much of a pain they actually are. – user6144226 Jun 06 '17 at 22:05
  • @WajdTohme If you are open to learning something new you should check out EntityFramework and Code First Databases, you will never look back. If you want some help there I can do that. – David Lee Jun 06 '17 at 22:05
  • I have to learn this because that's what the course requires. Otherwise I wouldn't touch C# and T-SQL together with a 5 feet pole. No offense. – wajd Jun 06 '17 at 22:07

1 Answers1

0

Make sure you are filling the Owners table before the Tigers table. In the same DataSet

For this line to execute with the constraint enabled

this.tigersTableAdapter.Fill(this.tRFDDataSet.Tigers);

You the this.tRFDDataSet.Owners table needs to have a row for every Owner with a MemberId in this.tRFDDataSet.Tigers. Meaning that you will have to create/drop in an OwnersTableAdapter and call its Fill Method before filling Tigers

user6144226
  • 613
  • 1
  • 8
  • 15