Re: Violation of PRIMARY KEY constaint...

Tech-Archive recommends: Fix windows errors by optimizing your registry



Most people use an identity column for their primary key.

What's killing you here is the SELECT COUNT. Your dataset tracks whether a
record is supposed to be inserted or updated. Do you think somebody is
going to come in and add the same record that the current user has added?
Is that really likely?

If you can rely on the dataset, you can set up an InsertCommand,
UpdateCommand, and DeleteCommand object for the data adapter, and the data
adapter will run the Insert command for Adds, and the Update command for
Changes and the DeleteCommand for deletes. You just call the Update method
of the TableAdapter once. It goes through the rows and processes them
accordingly.

Robin S.
-----------------------------
"Ken Abe" <KenAbe@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:34335C98-F0C1-4B6B-9AFA-721547EA2C71@xxxxxxxxxxxxxxxx
Thank you all for your input. Based on the assumption that
SqlCommandBuilder
is probably the culprit, I went a head an implement my own SP which
handles
both insert and update. It does a query on the two integers that make up
the
primary key and performs either an insert or an update accordingly:

if (select count(*) from t where key1 = @key1 and key2 = @key2) > 0
<update>
else
<insert>

All I achieved is to slow down the application. The Primary Key
constraint
violation still happens.

If aggregate primary keys are problematic, what's an alternative approach
to
solving this kind of problem, aside from not using ADO.net?


.



Relevant Pages

  • Re: .net table adapter code behind missing UPDATE statement
    ... Check to make sure that the TABLE you are working with has a PRIMARY KEY ... Once you get beyond this you are going to like working with .NET and Oracle ... is sql server this option is enabled). ... but not the Update command (note if the source db is sql ...
    (microsoft.public.dotnet.general)
  • RE: .net table adapter code behind missing UPDATE statement
    ... Check to make sure that the TABLE you are working with has a PRIMARY KEY ... Once you get beyond this you are going to like working with .NET and Oracle ... is sql server this option is enabled). ... but not the Update command (note if the source db is sql ...
    (microsoft.public.dotnet.general)
  • Optimistic Concurrency causing Data Adapter Configuration Wizard Error
    ... I have a simple database with a primary key and 25 columns. ... "There were errors configuring the data adapter." ... Concurrency" radio button on the Adavanced options of the wizard is ... The primary key is an INT type and the other fields are "text" type. ...
    (microsoft.public.dotnet.framework.adonet)
  • DataSet and updating
    ... I have a table with a primary key being an integer. ... it could not create an update command for my table. ... But after that record is added, it gives me a constraint error. ... It looks like it is attempting to insert copies of them into the table (which violates my primary key.) ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: Insert New Record Fails On Table With Unique Constraint
    ... because two other columns are set as the primary key. ... "carion1" wrote: ... >> used Visual Studio IDE SQL Data Adapter Wizard to create the stored ...
    (microsoft.public.dotnet.languages.csharp)