ConstraintException was unhandled

adshocker

Well-known member
Joined
Jun 30, 2007
Messages
180
Programming Experience
Beginner
Hi all,

Just wondering what triggers this exception error. I keep getting it when retrieving data but not when sending data back to the database.

Here's my situation.. I have a dataset.xsd. have a few datatables and adapters. the problem occurs in my datatable that has 2 datarelation.

I was advised to turn off enforce constraints but I'm not sure it thats a good idea.

I've also tried clearing dataset before filling datatables but same error.

I'm also not doing any duplicate data for the primary key since I only get the error when filling the datatable. If i dont fill the datatable of Form_Load and directly do a BindingSource.AddNew, I am able to save that record with no errors.

any other reason why this exception occurs?

thanks.
 
What is the exception text?

This might happen because youre filling a child table with a value that isnt in the parent table
 
It says "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

I've verified that the Parent Table is being Filled first before the Child Table.

Also, is it ok for a Child Table to have 2 different Parent Table?

Here's the sample screen shot of what i Have

relation.jpg


rental is set to both foreign key constraint and relation. using tran_id, cust_no
rented is set to relation only. using video_id

I have 2 screens....
First screen called View Rented Videos... this screen only allows me to see who rented a particular video and some other info like date rented, date due, returned etc...

Second Screen is Rental Transaction... this is where i create transaction records... I can select which customer is renting in the parent table... then save parent table... then add records on child table which has the video id and all the other info.
 
Last edited:
Just an idea, maybe you fill the child tables first, but you also clear them first... When you fill them, try clearing them in reverse order (parent, child) then filling them (child, parent).
 
hi,
i tried what you suggested but i still keep getting the error.

also for additional info.

to explain further... i get this error everytime i call the view rented video screen... on my view rented video screen, this is all i have

VB.NET:
Private Sub rv01_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SqlDataset.customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.SqlDataset.customer)
'TODO: This line of code loads data into the 'SqlDataset.video' table. You can move, or remove it, as needed.
Me.VideoTableAdapter.Fill(Me.SqlDataset.video)
'TODO: This line of code loads data into the 'SqlDataset.rented_video' table. You can move, or remove it, as needed.
[I]Me.Rented_videoTableAdapter.Fill(Me.SqlDataset.rented_video)[/I]
End Sub
the exception occurs on the italized line.

on my rental transaction screen i have these codes... but i dont get any constraintexception error here.

VB.NET:
Private Sub rp01_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SqlDataset.rental_payments' table. You can move, or remove it, as needed.
Me.Rental_paymentsTableAdapter.Fill(Me.SqlDataset.rental_payments)
'TODO: This line of code loads data into the 'SqlDataset.rented_video' table. You can move, or remove it, as needed.
Me.Rented_videoTableAdapter.Fill(Me.SqlDataset.rented_video)
'TODO: This line of code loads data into the 'SqlDataset.customer' table. You can move, or remove it, as needed.
Me.CustomerTableAdapter.Fill(Me.SqlDataset.customer)
'TODO: This line of code loads data into the 'SqlDataset.video' table. You can move, or remove it, as needed.
Me.VideoTableAdapter.Fill(Me.SqlDataset.video)
Me.RentalpaymentsBindingSource.AddNew()
End Sub
 
i got it working now... apparently i needed to fill both parent tables in both screens regardless of which table i am and i am not using on the screen.

thanks for the replies.
cheers.
 
It says "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

I've verified that the Parent Table is being Filled first before the Child Table.

Also, is it ok for a Child Table to have 2 different Parent Table?

No, because youre implying that youre only filling one parent table. Unless you fill both parents first, and ensure that each parent contains a record for all the children you'll get a ConstraintException

I had a similar issue; i use one address table for all entities in my database that might have an address, like a bank, customer, supplier. THe problem was.. My customers form only dealt with tblCustomers and tblAddress. One customer can have many address, so address was the child table. When I was filling it I was getting constraint exceptions becuas ehte other tables present in the dataset (unused) of bank and supplier didnt have a parent record for that address. Eventualy, to reuse the dataset rather than split it, I programmatically removed whichever constraints i didnt need on a given form. I now make separate datasets per form.

Here's the sample screen shot of what i Have

relation.jpg


rental is set to both foreign key constraint and relation. using tran_id, cust_no
rented is set to relation only. using video_id

I have 2 screens....
First screen called View Rented Videos... this screen only allows me to see who rented a particular video and some other info like date rented, date due, returned etc...

Second Screen is Rental Transaction... this is where i create transaction records... I can select which customer is renting in the parent table... then save parent table... then add records on child table which has the video id and all the other info.
I would guess hten, that you making the same assumption I was, and forgetting that the relation still exists in the dataset even though youre not using it on a particular form.. If you fill the Video screen with info from rented_video and video, then you must either also include all the people who ever rented it, or disable the contraint link to rental payments
 
Also, you might want to search the forums for Nitpick.dll - its a thing I wrote to help Arg81 with an issue.. You pass in a dataset and it tells you what is wrong with it, what rows are in error in what table etc
 
Just an idea, maybe you fill the child tables first, but you also clear them first... When you fill them, try clearing them in reverse order (parent, child) then filling them (child, parent).

Erm. Children must always be filled last and cleared first, otherwise you make orphans out of them both times
 
Can I also just point out that your profile and signature say .NET 1.1 but that shotyou posted was definitely .NET 2
 
I would guess hten, that you making the same assumption I was, and forgetting that the relation still exists in the dataset even though youre not using it on a particular form.. If you fill the Video screen with info from rented_video and video, then you must either also include all the people who ever rented it, or disable the contraint link to rental payments

this was exactly my situation.
thanks for the info.

sorry about my signature... just recently upgraded to VS 2005. forgot to update my sig. =)
 
also you see the words under your name? they still say 1.1 - its in your control pane;l somewhere.. i forget where
 
pps; i find that disabling the constraints that are not in use is better than downlaoding huge amounts of data i wont ever use :D
 
good point.
 
Back
Top