DataTable.LoadDataRow Purgutory

  • Thread starter Thread starter James
  • Start date Start date
J

James

Hi and thanks in advance,

I am getting a violation of PRIMARY KEY constraint exception when
calling update on a data adapter, after using LoadDataRow(). This is
what I am doing:

1) Fill dataset and AcceptChanges
2) Build object array and populate with correct types
3) Call LoadDataRow on the datatable passing in the array

Now at this point the row is getting created in the DataTable when there
is an existing row with the same primary key. This is not what the
documentation prescribes - it ostensibly should not add duplicates.

4) If the dataset HasChanges() call Update on a dataadapter passing in
the dataset.

Calling update throws the exception.

Many thanks for any help in this extremely trying time :)
 
Hi James,

Does the DataTable have primary key row defined?
I think not - adapter relies on the DataTable.PrimaryKey.
You might create strong typed dataset and then use it (recommended) instead
of untyped one.
 
Miha said:
Hi James,

Does the DataTable have primary key row defined?
I think not - adapter relies on the DataTable.PrimaryKey.
You might create strong typed dataset and then use it (recommended) instead
of untyped one.

Hi Miha,

The datatable has a primary key made up of five datacolumns. The dataset
is strongly typed.

James
 
Interesting.
Is DataSet.EnableConstraints == true?
Primary key should enforce unique values.
Is there autoinc field within key?
Can you show us the structure of dataset?
 
Miha said:
Interesting.
Is DataSet.EnableConstraints == true?
Primary key should enforce unique values.
Is there autoinc field within key?
Can you show us the structure of dataset?

I have fixed it, thanks for your input. Basically what was happening was
one of the key fields was getting truncated at the insert to the
database, which the dataset didnt' know about, so LoadRowData was adding
the row in question anyway which, when truncating again at the insert
threw the exception. Thanks for your assistance though. I dont think
there a way to enforce column length in datasets?
 
there a way to enforce column length in datasets?

Nope, but you could implement ColumnChanging event and truncate the field by
yourself (e.ProposedValue).
 
Back
Top