您的位置:首页 > 产品设计 > UI/UE

DataSet hell - "Failed to enable constraints. One or more rows contain values...."

2011-08-24 16:55 477 查看
Important note: Hi folks. This post was written in 2004. Yes, people have been getting this for over 6 years. It’s 2011 now, and here you are, still stuck working on this crap.
I highly suggest you read this, as an alternative rout to solve your problems.

Thanks, Roy in 2011.
================================

A friend asks:

When I fill my dataset I get this:
“Failed to enable constraints. One or more rows  contain values violating non-null, unique, or foreign-key  constraints.”

As you can see, “one or more” is not very informative. How do I identify the criminal ?

Since the only solution I know is to go through each table and compare it’s constraints,

Is there a more efficient way to solve this ?

I'll admit this is one of the more annoying problems I've encountered when working with the Dataset object.

And no - I don't know an easy way to find out who the culprit is. Do you? Please share in the comments.

Update (plenty of good comments to this post so I pasted them all in at the bottom- make sure to read through them..):

In the comments to this post, Sanjay gives a good answer to this problem:

“The solution lies in the GetErrors method of the DataTable class:
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDataDataTableClassGetErrorsTopic.asp

Essentially, when I see the message above, I check the HasErrors property for each DataTable in the data set and then invoke the GetErrors method on the tables reporting errors. The GetErrors returns a collection of DataRows and you can invoke the .RowError
property on the each of the error-stricken rows to find out just exactly what the problem is . ”         

.”..I forgot to add the part about turning off the .EnableConstraints before the Fill. I normally enable the constraints again, only after the original Fill is complete. Then, you can catch the exception above and launch a error detection function
which does what I described above. “

Thanks, Sanjay! Lots of other good comments and differetn solutions as well…
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐