
Question:
I am loading a data table from an XSD data set in C# in MSVS 2012. The standard GetData()
method runs properly. However, my custom query GetDataCustom()
, which returns only a few fields from the data set, throws the popular exception:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
</blockquote>I used the Detailed Constraint Exception posted in reply to a similar issue here on SOV, which showed this detail:
Error filling table
No Row Errors reported in DataTable=[datatable_Custom]
I set EnforceConstraints = false
on my data set, but the exceptions still were thrown. I have looked through the many questions on SOV on this issue, but none of the other fixes helped. What else can I try?
Edited to add code block:
ds_Sample dsSample = new ds_Sample();
dsSample.Clear();
dsSample.EnforceConstraints = false;
ds_SampleTableAdapters.ta_Sample taCustom = new ds_SampleTableAdapters.ta_Custom();
ds_Sample.dt_CustomDataTable dtCustom = taCustom.GetDataCustom();//throws exception
Answer1:Fixed it myself: The table adapter had all the fields in the data set, but my query only had some of the fields in the table adapter. I changed the table adapter to use the query of interest, and the problem was resolved.
Therefore, the constraint violation was most likely null data in NOT NULL
fields.