Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2293
  • Last Modified:

A first chance exception of type 'System.Data.ConstraintException' occurred in System.Data.dll

I use a strongly typed Dataset with TableAdapters. Upon activating the foreign key constraint for the relation between two tables (parent/child, 1:n) and filling the TableAdapter, I get the following error:

A first chance exception of type 'System.Data.ConstraintException' occurred in System.Data.dll
row 0 error: ForeignKeyConstraint FK_Events_Customer_Event requires the child key values (5fcb63af-000d-4f93-94a1-5b867d0a03f0) to exist in the parent table.

THIS IS A WRONG EXCEPTION. The child key value exists in the parent table.
Activating the same constraint on the database itself (SQL Server) gives NO ERRORS.
Activating the same constraint in ADO.Net 1.1 works as well.

Is this a bug in ADO.Net 2.0? Thanks for your help!

Best regards,
Chris
0
sintec_ch
Asked:
sintec_ch
  • 3
  • 2
1 Solution
 
dave4dlCommented:
can you post the relevant portions of your code?
0
 
sintec_chAuthor Commented:
well, no coding work involved, just 2 data tables (1:n / Parent:Child), a strongly typed dataset, 1 tableadapter.

The exception is thrown upon calling the tableadapter's Fill-Method if the foreign key constraints in the datatables are activated. (relation only works fine).

Thanks for your time,
Chris
0
 
dave4dlCommented:
Not sure what is going on to cause constraint to be violated but it could be that when you activate it via the database you are not telling it to validate existing data but perhaps the default behavior when activating it via the TableAdapter (the behavior may also have been different in ado.net 1.1).

Some other questions:
Do you have any nulls in either of your key columns?
Do you are your key columns character type?  If so make sure there are no trailing or leading whitespace/hidden characters in your codes.
Does the problem occur with no records?  With just one record in each table?

Possible work around:
Since things seem to work correctly in the database when issuing SQL commands against it, execute (via ADO.NET) the command to disable the foreign key, do your work with the table adapter, then execute the statement to re-enable the constraint.  If disabling it does not work, just remove the constraint and re-add it.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dave4dlCommented:
hmm, didnt quite finish one of my sentences but i think you got my general point
0
 
sintec_chAuthor Commented:
I solved the problem.

I overlooked something: The assistent places the TableAdapter-Fill methods in the Form_Load EventHandler. But it doesn't care of the order of the Fill-Methods.

If the Fill-Method of the Child-TablerAdapter is called before the MasterTable's Fill method a Constraint Exception is thrown.

Due to the absence of data in the (Datasets) MasterTable the ParentRecords are missing and the constraint exception is thrown.

Conclution: Call the Fill-Methods in the correct order (Master Table first) or disable Check Constraints temporarily.

Thanks for your help anyway!

Chris
0
 
Computer101Commented:
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now