Violation of PRIMARY KEY

N

Nime

Hi, I want to remove the row then add the fresh new one, so I don't have to
decide to update or add.
I always add, because I delete any existing row.

However I'm stuck with the error below:
Violation of PRIMARY KEY constraint 'PK_dtotel'. Cannot insert duplicate
key in object 'dtOtel'.

dtOtelTA.Fill(dtOtelDT); //Load the data
try {
//Find the row then delete if exists
dtOtelRow = dtOtelDT.FindBykayitID(intAktifKayýtID);
dtOtelDT.RemovedtOtelRow(dtOtelRow); //Remove it
dtOtelTA.Update(dtOtelDT); }
catch (Exception ex) { };
//Now create a new one:
dtOtelRow = dtOtelDT.NewdtOtelRow();
dtOtelRow.kayitID = intAktifKayýtID; //Foreign key
dtOtelDT.AdddtotelRow(dtOtelRow); //Add row
dtOtelTA.Update(dtOtelDT); //Update the database

It looks like the row never has been not deleted.
Do I miss anything? Thanks...
 
N

Nicholas Paldino [.NET/C# MVP]

Nime,

You can't call RemovedtOtelRow because that will remove the row. This
will actually remove the row from the DataTable. This is a bad thing,
because the row has to remain in the table in order for the data adapter to
know it has to be deleted.

What you want to do is get the row and then call the Delete method on
it, and then call Update.

I can't say that I think that deleting the record and then adding it
back is the best idea, quite frankly. It's a lot of overhead (extra calls
to the database) that you simply don't need.
 
M

Mr. Arnold

Nime said:
Hi, I want to remove the row then add the fresh new one, so I don't have
to decide to update or add.
I always add, because I delete any existing row.

However I'm stuck with the error below:
Violation of PRIMARY KEY constraint 'PK_dtotel'. Cannot insert
duplicate key in object 'dtOtel'.

dtOtelTA.Fill(dtOtelDT); //Load the data
try {
//Find the row then delete if exists
dtOtelRow = dtOtelDT.FindBykayitID(intAktifKayýtID);
dtOtelDT.RemovedtOtelRow(dtOtelRow); //Remove it
dtOtelTA.Update(dtOtelDT); }
catch (Exception ex) { };
//Now create a new one:
dtOtelRow = dtOtelDT.NewdtOtelRow();
dtOtelRow.kayitID = intAktifKayýtID; //Foreign key
dtOtelDT.AdddtotelRow(dtOtelRow); //Add row
dtOtelTA.Update(dtOtelDT); //Update the database

It looks like the row never has been not deleted.
Do I miss anything? Thanks...

Yeah, you have missed not to be doing it this way. You either add a new
record or update an existing record.

Not this nonsense you're trying to do.
 
P

pvdg42

Nime said:
Hi, I want to remove the row then add the fresh new one, so I don't have
to decide to update or add.
I always add, because I delete any existing row.

However I'm stuck with the error below:
Violation of PRIMARY KEY constraint 'PK_dtotel'. Cannot insert
duplicate key in object 'dtOtel'.

dtOtelTA.Fill(dtOtelDT); //Load the data
try {
//Find the row then delete if exists
dtOtelRow = dtOtelDT.FindBykayitID(intAktifKayýtID);
dtOtelDT.RemovedtOtelRow(dtOtelRow); //Remove it
dtOtelTA.Update(dtOtelDT); }
catch (Exception ex) { };
//Now create a new one:
dtOtelRow = dtOtelDT.NewdtOtelRow();
dtOtelRow.kayitID = intAktifKayýtID; //Foreign key
dtOtelDT.AdddtotelRow(dtOtelRow); //Add row
dtOtelTA.Update(dtOtelDT); //Update the database

It looks like the row never has been not deleted.
Do I miss anything? Thanks...
Yep, you missed all the extra work you're creating for the DBMS by
unnecessarily deleting rows, then re-adding when all that's needed is a
simple update query if the row exists. I agree with Nicholas 1000% that this
is a bad idea. Talk to your DBA and let her/im explain the difference.
You're checking to see if the key exists already. If it does, update the
existing row.
 
N

Nime

Yes you are right, I used the row Delete method instead:

dtOtelTA.Fill(dtOtelDT);
try {
dtotelRow = dtOtelDT.FindBykayitID(intAktifKayýtID);
dtotelRow.Delete();
dtOtelTA.Update(dtOtelDT);
}
catch (Exception ex) { };

haber said:
Nime,

You can't call RemovedtOtelRow because that will remove the row. This
will actually remove the row from the DataTable. This is a bad thing,
because the row has to remain in the table in order for the data adapter
to know it has to be deleted.

What you want to do is get the row and then call the Delete method on
it, and then call Update.

I can't say that I think that deleting the record and then adding it
back is the best idea, quite frankly. It's a lot of overhead (extra calls
to the database) that you simply don't need.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nime said:
Hi, I want to remove the row then add the fresh new one, so I don't have
to decide to update or add.
I always add, because I delete any existing row.

However I'm stuck with the error below:
Violation of PRIMARY KEY constraint 'PK_dtotel'. Cannot insert
duplicate key in object 'dtOtel'.

dtOtelTA.Fill(dtOtelDT); //Load the data
try {
//Find the row then delete if exists
dtOtelRow = dtOtelDT.FindBykayitID(intAktifKayýtID);
dtOtelDT.RemovedtOtelRow(dtOtelRow); //Remove it
dtOtelTA.Update(dtOtelDT); }
catch (Exception ex) { };
//Now create a new one:
dtOtelRow = dtOtelDT.NewdtOtelRow();
dtOtelRow.kayitID = intAktifKayýtID; //Foreign key
dtOtelDT.AdddtotelRow(dtOtelRow); //Add row
dtOtelTA.Update(dtOtelDT); //Update the database

It looks like the row never has been not deleted.
Do I miss anything? Thanks...
 
N

Nime

I got tons of subtables and it's not a good idea to write extra code.
I just simply delete any existing row...
I keep the code much simple and easy to read.
 
P

pvdg42

Nime said:
I got tons of subtables and it's not a good idea to write extra code.
I just simply delete any existing row...
I keep the code much simple and easy to read.
Again, you should talk to whoever is administering the database to find out
why your approach is not a good idea.
If anybody associated with your system is at all concerned with efficiency,
they should be able to explain it to you.
 

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