Solved

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

Posted on 2007-04-06
7
2,164 Views
Last Modified: 2008-09-12
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
Comment
Question by:sintec_ch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
7 Comments
 
LVL 15

Expert Comment

by:dave4dl
ID: 18865153
can you post the relevant portions of your code?
0
 

Author Comment

by:sintec_ch
ID: 18872962
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
 
LVL 15

Expert Comment

by:dave4dl
ID: 18873720
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 15

Expert Comment

by:dave4dl
ID: 18873722
hmm, didnt quite finish one of my sentences but i think you got my general point
0
 

Author Comment

by:sintec_ch
ID: 19022373
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19280640
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question