Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > System.Data.ConstraintException
 

System.Data.ConstraintException

In my first ADO.Net 2.0 project I'm trying to create a Windows Forms application that allows updating customer records. These records are in an SQL Server 2003 database, and I am using the VS.Net 2005 DataSet Designer to create a typed DataSet and Customer TableAdapter. To start, I just output the internal IDs of fields to TextBoxes on a form. So far, so good.

One of the fields is for Language (English, French, Spanish, etc.), and there is a database table with language names and IDs. So I add a new Language TableAdapter and Relation to the DataSet Designer. The Relation is:

Parent table: Language
Child table: Customer
Key Column: Language.languageID
Foreign Key Column: Customer.languageID
Both Relation and Foreign Key constraint
Update Rule: Cascade
Delete Rule: Cascade
Accept/Reject Rule: None

Previewing all TableAdapters look fine, and all customers have valid languageIDs. I create a Language ComboBox in my form, choose "Use Data bound items" and... where's Language? There are a few other tables I'm going to be using, but no Language. There's even another relation, but not the language one. Well, I'll leave it unbound for now, run it, and...

A first chance exception of type 'System.Data.ConstraintException' occurred in System.Data.dll
...
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

It lies; all rows contain valid unique data, and no constraint violations. I can run the SELECT commandText in all DataAdapters on the database without errors. This only started to happen after I added the Language table.

What am I doing wrong?
Dour High Arch  Wednesday, July 26, 2006 12:03 AM
I figured it out.

When I added the Language table and comboBox to my form, VS helpfully added the languageTableAdapter to my Form_Load method:

this.customerTableAdapter.Fill(this.retailerDataSet.Customer);
this.languageTableAdapter.Fill(this.retailerDataSet.Language);

But Language is parent to Customer and has a primary key relation. When Customer gets loaded Language hasn't been, and there are no foreign keys for Customer.languageID. How did I fix this? Simply load Language first:

this.languageTableAdapter.Fill(this.retailerDataSet.Language);
this.customerTableAdapter.Fill(this.retailerDataSet.Customer);

The try/catch method worked because in my catch clause I loaded Language first. Proof again that design-time controls are no benefit if you don't understand the technology.
Dour High Arch  Thursday, July 27, 2006 5:11 PM

the following code is weird but works.

my fillby returnsa schema different from the fill schema- the default oneu can call it.

and i am getting the constraints exeption. need to catch and clear it again it seems dont ask me why. see if this brings you somewhere

Me.CostingDataSet.Costing_Bill.Constraints.Clear()

Try

Me.Costing_BillTableAdapter1.FillBy(Me.CostingDataSet.Costing_Bill)

Catch r As ConstraintException

Me.CostingDataSet.Costing_Bill.Constraints.Clear()

Me.Costing_BillTableAdapter1.FillBy(Me.CostingDataSet.Costing_Bill)

End Try

hrubesh  Wednesday, July 26, 2006 12:49 PM
Ugh that is bad, like "fixing" a blown fuse by putting a jumper on it. But ya know what? It worked, the second time I try the Fill it doesn't throw.

I think it has something to do with my LanguageTableAdapter; when I took that out and created a separate LanguageBindingSource, the new binding source shows up as a data source for the comboBox, gets filled with all languages, and throws no exceptions. The problem is that this completely disconnects it from my Customer table; it does not show the customer's language and changing it has no effect on Customer.languageID.

Man, I do not understand this stuff. Can someone suggest documentation or a tutorial that shows updating foreign keys from a Forms app?
Dour High Arch  Wednesday, July 26, 2006 6:31 PM
I figured it out.

When I added the Language table and comboBox to my form, VS helpfully added the languageTableAdapter to my Form_Load method:

this.customerTableAdapter.Fill(this.retailerDataSet.Customer);
this.languageTableAdapter.Fill(this.retailerDataSet.Language);

But Language is parent to Customer and has a primary key relation. When Customer gets loaded Language hasn't been, and there are no foreign keys for Customer.languageID. How did I fix this? Simply load Language first:

this.languageTableAdapter.Fill(this.retailerDataSet.Language);
this.customerTableAdapter.Fill(this.retailerDataSet.Customer);

The try/catch method worked because in my catch clause I loaded Language first. Proof again that design-time controls are no benefit if you don't understand the technology.
Dour High Arch  Thursday, July 27, 2006 5:11 PM

You can use google to search for other answers

Custom Search

More Threads

• adding rows to a datagridview having problems
• DataGridView vertical scrollbar not appearing
• IndexOutOfRangeExceptionwhile deleting multiple rows from a bound DataGridView
• How to update one field in a few rows in the Datagrid?
• Need a scrollbar in datagridview when it's readonly
• Custom databinding
• Combobox use in visual studio
• Storing a computed column back to the database
• I've got confused by overriding OnDrawItem in DataGridViewComboBoxEditingControl, somebody please help me~!
• How do I get the contents (i.e text) of a cell into a string?