views:

1048

answers:

3

Hi,

I have a weird problem where the "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error sometimes pops up when I am trying to build my project.

The line in question throwing the error was auto-generated code in the designer file for the dataset. To find out the offending table, I used the following code:

      Try
            Me.Adapter.Fill(dataTable) <--Breakpoint here on the offending line

        Catch ex As Exception

            For Each TRow As DataRow In dataTable.Rows
                If TRow.HasErrors Then
                    Trace.Write(TRow.RowError)
                End If
            Next

        End Try

Funnily enough, as soon as I run the project after putting in the above code and breakpoint, the error disappears. I assume this has something to do with the code being regenerated. All data is presented successfully, and the project compiles without errors.

However, this has happened often enough for me to frustrate me. Anybody know what might be causing the error and how I can isolate it?

A: 

What unique constraint are you placing on the datatable and have you verified that the data your passing in doesn't have any duplicates? If this is coming from a shared database perhaps someone else is modifying the data which could be causing the randomness.

Edit

If that code is exactly as what you have (And forgive me my vb.net is not as strong as my C#). Wouldn't it swallow the exception? In C# we need to rethrow the error.

Add a Trace.Write() right before you go into your for loop.

JoshBerke
I have examined the constraints on the tables, but haven't found any data violating them. More importantly, note that the error disappears when I add the above code and set the breakpoint. The project then runs without a hitch, and all data is presented.
Wild Thing
The database isn't shared, and I have primary keys on all tables. I do have indexes on some tables, though.
Wild Thing
About the code, it's not intended to be used in production - I just use it to set a breakpoint to find out which table is causing the error. Using this code, I can find out exactly which data is causing the error. When I use it on this particular piece of code, though, the breakpoint isn't triggered
Wild Thing
Ohh so the breakpoint before you fill the data is not triggered? Weird without seeing it I'd guess an environment issue (the breakpoint not being hit), but I'm guessing you went through the usuall clean build, restart vs etc...so Im out of ideas.
JoshBerke
A: 

You are probably working with a typed dataset, or have some code configuration on it. Also you might be reading from one table, and then adding data from another.

In any of those cases, you really want to look into any inconsistencies between the schema. If it is a typed dataset / or configured in code, check its definition, and look for anything different from the sql table. If you are reading from 2 different tables, check the definition of both tables.

eglasius
If this were so, wouldn't an error be thrown every time?
Wild Thing
A: 

Figured it out finally!

The reason the breakpoint wasn't being hit was because the code in question was auto-generated, and had the attribute "DebuggerNonUserCodeAttribute". In addition, in my VS settings, I had set the debugger to "Just my code".

When I ran the code again, and the breakpoint wasn't hit, I noticed the breakpoint icon had changed to a warning. Hovering showed this:

"The breakpoint will currently not be hit. Breakpoints cannot be set in a method or class with the 'DebuggerNonUserCode' attribute when the debugger option 'Just My Code' is enabled."

Ouch!

Once I had removed the "Just My Code" debugging option, the breakpoint was hit, and I discovered that one of the rows had a NULL value in one of the columns which had a non-null constraint. Stupid ole' me!

Wild Thing