Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-04-06
7
Medium Priority
?
2,277 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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