DataTable.GetChanges() -> constraint violations

  • Thread starter Thread starter Brad Williams
  • Start date Start date
B

Brad Williams

If you have a DataSet with EnforceConstraints==false, and constaint
violating data in it, when you call GetChanges() on one of it's tables you
can get a constraint violation exception. Isn't this a bug? GetChanges()
on the same DataSet works fine.

Brad Williams
 
Hi Brad,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you received a ConstraintException when
trying to call GetChanges method on one of the tables in the DataSet,
although the EnforceConstraints property of the DataSet has been set to
false. If there is any misunderstanding, please feel free to let me know.

As far as I know, this behavior is by design. As the
DataSet.EnforceConstraints property works on DataSet level, it just ignores
the ConstraintExceptions thrown from the DataTables of that DataSet. When
calling GetChanges on a DataTable level, it creates a new DataTable object
with same constraints as the original table and this DataTable doesn't
belong to the DataSet. Since there are duplicate on primary keys, a
ConstraintException is thrown. So if you have had EnforceConstraints set to
false, try not to call GetChanges on the DataTable level.

HTH. If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks, Kevin, for response.

If what you described was true, I would expect that calling BeginLoadData()
on the original table within the original dataset before calling GetChanges
on that dataset would alleviate the problem, but even in that case I get a
NoNullAllowedException if a non-nullable field of that table had a NULL in
it. Here is my code:


this.ThisDataSet.EnforceConstraints = false;
this.ThisDataSet.Tables[tableName].BeginLoadData();
bool hasTableChanged = false;
try {
if (this.ThisDataSet.Tables[tableName].GetChanges() != null)
hasTableChanged = true;
}
catch (NoNullAllowedException) {
hasTableChanged = true; // HIT HERE
}
catch (ConstraintException) {
hasTableChanged = true;
}

Interestingly, I can see in the debugger at the HIT HERE spot that
this.ThisDataSet[tableName].enforceConstraints==true while
this.ThisDataSet[tableName].EnforceConstraints==false. Neither
field/property is accessible, I just see this in debugger. I was expecting
this.ThisDataSet.Tables[tableName].BeginLoadData() to disable constraint
checking at the table level.

In summary, I think that if myTable.GetChanges() blows with
NoNullAllowedException immediately after myTable.BeginLoadData(), then
that's a bug. Is this wrong?

Brad Williams
 
Hi Brad,

Sorry that I didn't express myself clearly in my last post. Since the
DataTable.GetChanges method returns a DataTable object, it was the RETURNED
DataTable object which throws this exception.

When setting DataSet.EnforceConstraints to false, ConstraintExceptions from
DataTable level will be caught and swallowed. However, the non-accessable
values for enforceConstraint in DataTables are still true. So when you call
GetChanges on DataTable level, the method will create another DataTable
object with enforceConstraint value = true. And that newly created
DataTable object throws exception. So it doesn't take effect whether you
call BeginLoadData on the original DataTable.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Brad,

I forgot to mention that it is by design, not a bug.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thank you, Kevin, I understand now that DataTable.BeginLoadData does not
turn off constraint enforcement on the table in the way that I was
expecting.

Here's a follow up: So is there nothing we can call that sets the internal
flag DataTable.enforceConstraint to false?

Brad Williams
 
Hi Brad,

Since the DataTable.enforceConstraint is an private variable of the
DataTable object. It seems that we cannot set it to false.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Yes, not directly. My question is: is there anything that we do that ever
has the side-effect of setting it to false? If there is, I haven't found
it.
 
Hi Brad,

I don't think there is side-effect of setting DataSet.EnforceConstraints to
false. If you set it to false, you had better not to call GetChanges on
DataTable level.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi Brad,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top