Solved

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

Posted on 2007-04-06
7
2,136 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

772 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