473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Violation of PRIMARY KEY

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...
May 8 '07 #1
6 2868
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]
- mv*@spam.guard.caspershouse.com

"Nime" <ni**@yes.nowrote in message
news:OC*************@TK2MSFTNGP06.phx.gbl...
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...

May 8 '07 #2

"Nime" <ni**@yes.nowrote in message
news:OC*************@TK2MSFTNGP06.phx.gbl...
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.

May 8 '07 #3

"Nime" <ni**@yes.nowrote in message
news:OC*************@TK2MSFTNGP06.phx.gbl...
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.
May 8 '07 #4
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) { };

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>, haber
iletisinde şunları yazdı:%2***************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Nime" <ni**@yes.nowrote in message
news:OC*************@TK2MSFTNGP06.phx.gbl...
>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...


May 9 '07 #5
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.

"pvdg42" <pv****@newsgroups.nospam>, haber iletisinde şunları
yazdı:Os**************@TK2MSFTNGP05.phx.gbl...
>
"Nime" <ni**@yes.nowrote in message
news:OC*************@TK2MSFTNGP06.phx.gbl...
>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.

May 9 '07 #6

"Nime" <ni**@yes.nowrote in message
news:O8**************@TK2MSFTNGP03.phx.gbl...
>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.
May 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Edward | last post by:
Access 97 SR 2 Front End SQL Server 7.00.1063 Back End When I attempt to relink the tables in my Access app. I am suddenly getting this weird error message = "ODBC--Call Failed", followed by...
6
by: J Smith | last post by:
After doing some googling through the lists and such, I wasn't able to arrive at a solution for a problem I've run into recently. I've seen mention of similar symptoms, but my case seems different....
4
by: Steven Nagy | last post by:
Hi Have a problem that consistantly occurs in my applications where using a DataAdapter (OLEDB) and a dataset. Using a simple process of adding a row to the dataset table and then calling the...
2
by: John | last post by:
The ASP application inserts transaction records in transaction table with the system time as the primary key. However, it is possible to have primary key violation because the records in...
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
2
by: embarkr | last post by:
I am getting the error: "Violation of PRIMARY KEY constraint 'PK_tblCustomsTariffTreeMap'. Cannot insert duplicate key in object 'dbo.tblCustomsTariffCodeTreeMap'." However, the record I am...
1
by: Zamdrist | last post by:
Violation of PRIMARY KEY constraint 'PK_CUSTOM2'. Cannot insert duplicate key in object 'MHGROUP.Custom2' Is there ANY other reason this violation of the primary key would happen OTHER than a...
1
by: Walter Thizo | last post by:
Morning Programmers I have given project where there is corrupt database in the form of text file , what they need is paradox Boland c++ database that will handle the duplicates of student no...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
1
by: crichard1983 | last post by:
I'm using a Unique Key constraint for the first time, and I think I may not be understanding something correctly. I have a table with a Primary key (media_format_id) that is auto-increment. Then I...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.