?
Solved

Auto increment / key violation

Posted on 2006-05-28
12
Medium Priority
?
707 Views
Last Modified: 2010-04-05
Hi

My problem is that when I am deleting a record from a table based on an Auto Increment key it's giving me a KEY Violation error message
How can I solve this problem should I change the key ?

By the way I am using this table and it's query to populate a listview i am using in my program

regards
0
Comment
Question by:ibrobar
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 5

Expert Comment

by:Scay7
ID: 16779552
Bit more information is needed for us to understand the problem...

What type of database?
what version of delphi.
Snippet of code that gives the code violation or area of code that is used to delete the record.

Scay7
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16782117
does your table have any "triggers" or "events" that may be firing when you do the delete?
I'd start searching there first
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16783232
Try this scenario
1. Close the table/query.
2. delete the record.
3. reopen and populate your data
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Expert Comment

by:atul_parmar
ID: 16783335
The problem might be with indices. So, if the indices are corrupted then you can see such type of problem.

Reindex and try.
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16783408
Yeah, it is bug with Paradox. you need Pdxrbld - Paradox tables check/repair utility.
You can find it http://www.torry.net/authorsmore.php?id=1728
0
 

Author Comment

by:ibrobar
ID: 16783766
Dear All I am using Paradox table and Delphi version 4
I am filling a listview from this table I mentioned earlier and the key is Auto increment so when I am deleteing an item from the listview i am deleting it from the table assigned to. so when i am doing the delete it's giving me this error message Key violation . i think i cann't delete from an autoincement key table cause it will make gaps in the records
so any solution

regards
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16783817
You didn't get my point.

In paradox when you use AutoIncrement, it uses secondary index to maintain autoincrement. Now for some reason it fails to track the autoincrement and raises this error. The solution is NOT to use the autoincrement and design your own increment routine for this purpose.

See http://www.dbforums.com/archive/index.php/t-386182.html

0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16785740
Have you edited the table's data at all before trying to delete it??
Try doing a "Table1.Cancel;" before the "Table.Delete;"
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16787641
usual a delete will not cause a keyviolation,
except there is a constraint to a childtable

what is your exact errormessage?

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16787653
>In paradox when you use AutoIncrement, it uses secondary index to maintain autoincrement.

is of course nonsens, there is no secondary index until someone explicite define/create it
(the autoinc-value is maintained in the header part of the table-file)

meikl ;-)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 2000 total points
ID: 16787680
just for clarify my comment above (if someone may worry about i said nonsens)

from my paq for (re)-setting a autoinc,
with a bit adjustments you can also read
the current(last given) value.
(in that case the table must not be closed, the filestream-filemode should then be fmOpenRead + fmShareDenyNone)

//sets a autoinc-value
Procedure SetAutoInc(filename : string; Value : Longint);
var
  mystream : tfilestream;
begin
  mystream := tfilestream.create(filename,
              fmOpenWrite + fmShareExclusive);
  try
    mystream.Seek(73, soFromBeginning);
    mystream.Writebuffer(Value, SizeOf(Value));
  finally
    mystream.Free;
  end;
end;

//sample
procedure TForm1.Button1Click(Sender: TObject);
begin
  Table1.Close;  //must be closed
  SetAutoInc('C:\Test\Test.db',StrtoInt(edit1.text));  //for reset 0
  Table1.Open;
end;

meikl ;-)
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 16806176
>>I am filling a listview from this table I mentioned earlier and the key is Auto increment so when I am deleteing an item from the listview i am deleting it from the table assigned to.

Try to delete a record with out filling the list view(is it dbcomponent?)
What is happening?

Post your code

sun4sunday
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question