Solved

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

Posted on 2007-04-06
7
2,075 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now