Unique Ke Violation

J

Jim Heavey

Hello, I have run into something that I can not explain or figure out how
to overcome. I am getting the following error when when I do a "fill"
method of the dataAdapter - "Failed to enable constraints. One or more rows
contain values violating non-null, unique, or foreign-key constraints."

Through a process of elimination, but eliminating all foreign keys, making
all fields accept nulls, I have determined that by removing the primary key
on this table, the table loads just fine. So now I have all of the foreign
keys put back and each field does not allow nulls.

How could this be? I am pulling my data from a SQL database which
identifies this field as the primary key. The field in question is a guid
field. Here is the code I used for defining this table...

DataTable dtSkaters = new DataTable("Skaters");
dtSkaters.Columns.Add("ClubID",typeof(Guid ));
dtSkaters.Columns.Add("FirstName", typeof(String));
dtSkaters.Columns.Add("MiddleName", typeof(String));
dtSkaters.Columns.Add("LastName", typeof(String));
dtSkaters.Columns.Add("SkaterID", typeof(Guid));
// set up primary key for Skaters Table
DataColumn[] pkSkaters = { dtSkaters.Columns["SkaterID"]};
dtSkaters.PrimaryKey=pkSkaters;

I have visible looked at the table in Query Analyzer and can see no
duplication, So why amd I getting this error?

Thanks in advance for your assistance!!!!!!!!!!!!
 
M

Miha Markic

Hi Jim,

You might enable primary key and after you get the error (and you ignore
it - try/catch), you should iterate the rows and checking their RowError
descriptions tu understand better where the problem is
 
M

mklapp

Hello,

I had something like this happen. I had two data
tables in a single data set and tried to load from 2
tables with a defined relationship in the dataset (you
mention no relationships).

The parent table was restricted by a type value not
contained in the Child table at all. When I filled the
child table, the error you mentioned was raised because I
was pulling in children for parents not selected.

If you have defined relationships, make sure you're
selection criteria are consistent.

Also,

According to "Accessing and Changing Relational Data"
in Sql Server Books Online -

A table can have multiple uniqueidentifier columns. One
uniqueidentifier column for each table may be specified
with the ROWGUIDCOL property. The ROWGUIDCOL property
indicates that the uniqueidentifier values in the column
uniquely identify rows in the table. The property does
not do anything to enforce this, however. The uniqueness
must be enforced through other mechanisms, such as
specifying the PRIMARY KEY constraint for the column. The
ROWGUIDCOL property is primarily used by SQL Server
replication.

mklapp
 
R

Roy

Another way to over come this is doing the following:
EnforceConstraints = False

HTH,

Roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top