This is an error I run into frequently and is frustrating because of course their is no indication of what constraint is being violated. So now I run through the three most frequent problems that will cause this:
- Having a primary key specified on the DataTable where a query can potentially return duplicates. Sometimes duplicates are unavoidable and so removing the not helpful primary key will sort this one out.
- Finding the columns that have been set to not allow nulls - again, this is sometimes unavoidable and so going through all of the columns and allowing nulls will fix this.
- Exceeding the size of a column. This happens if you have created the DataTable and then for some reason alter the column size in the actual database table. The DataTable will not automatically update even if you alter the main query.
Occassionally I have found that nothing you do will get rid of the problem other than creating the DataTable and queries in a new DataSet (a tedious exercise).