Board index » delphi » I incorrectly get Key Violation!! A truly spurious Key Violation

I incorrectly get Key Violation!! A truly spurious Key Violation

I know you must be thinking that I'm doing something wrong, and normally I'd
be inclined to agree. However, in this case, I believe the db table has
gotten into some state which cannot be repaired.

It's a very small table, on which I have a secondary index with 3 fields. I
am trying to append to this table. One of the index fields in the new data
record to be added has a value of 45. I can easily look through the 2 dozen
records in the table to see that there is NO 45 in there.

I execute the following code, and FindKey CORRECTLY does NOT find a matching
key.  There should NEVER be a Key Violation anyway, since the following code
will EDIT the record if a matching key is found!

So, the code gets to the Post and produces a Key Violatin anyway, and the
record can't be stored in the db!!

Can anyone help solve this Case of the Spurious Key Violation??  Thanks!!!!

  // Header is a TTable component, and H is a record structure containing
data
  Header.IndexName := 'HeaderInfo';
  if FindKey([H.RunDate,H.Run,H.ServerName])
    then Edit
    else Append;
  Header.FieldByName('Run Date').AsDateTime := H.RunDate;
  Header.FieldByName('Run').AsInteger := H.Run;
  Header.FieldByName('Server Name').AsString := H.ServerName;
  Header.FieldByName('Download Timestamp').AsDateTime := H.DateTime;
  Header.FieldByName('Software Version').AsString := H.Version;
  Header.Post;   // <<<---  get a Key Violation here!!
  result := Header.FieldByName('ID').AsInteger;

P.S. I still get this Key Violation despite rebuilding the table and
indexes. The only way I do NOT get a key violation is if I delete the table
entirely, and recreate the table. Then the new data goes in just fine. But I
need to keep the old data as well, so that alone is not a good solution.

Besides, I'd like to find out what is causing this spurious Key Violation.
But unless we find out, it will happen again!

 

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


"Joseph" <jgl...@paytotheorder.com> skrev i en meddelelse
news:3aad277d_2@dnews...

Quote
> I execute the following code, and FindKey CORRECTLY does NOT find a
matching
> key.  There should NEVER be a Key Violation anyway, since the following
code
> will EDIT the record if a matching key is found!

Does the table have any secondary unique indexes?

Finn Tolderlund

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


Don't know if it helps, but you can try this one:

if FindKey([H.RunDate,H.Run,H.ServerName])
then begin
            Edit;
            Header.FieldByName('Run Date').AsDateTime := H.RunDate;
            Header.FieldByName('Run').AsInteger := H.Run;
            Header.FieldByName('Server Name').AsString := H.ServerName;
            Header.FieldByName('Download Timestamp').AsDateTime :=
H.DateTime;
            Header.FieldByName('Software Version').AsString := H.Version;
            Header.Post;
        end
else AppendRecord([H.RunDate, H.Run, H.ServerName, H.DateTime, H.Version]);
result := Header.FieldByName('ID').AsInteger;

Alfred.

"Joseph" <jgl...@paytotheorder.com> schreef in bericht
news:3aad277d_2@dnews...

Quote
> I know you must be thinking that I'm doing something wrong, and normally
I'd
> be inclined to agree. However, in this case, I believe the db table has
> gotten into some state which cannot be repaired.

[snip]

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


Quote
"Joseph" <jgl...@paytotheorder.com> wrote in message news:3aad277d_2@dnews...
> I know you must be thinking that I'm doing something wrong, and normally I'd
> be inclined to agree. However, in this case, I believe the db table has
> gotten into some state which cannot be repaired.

If it's pardox or Dbase, try doing a DBIDoRestructure.  Look in the BDE help file. fo this
function

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


I assume you are using Paradox tables. Is your secondary index unique? If
so, the key violation may come from the secondary index and not the primary
key. If that is not the case try the following. Create a new table from
scratch. In DBD choose Tools | Utilities | Add and add the records from the
original table to the new table. Now try adding the record that produced the
error to the new table and see what happens.

--
Bill

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


The question should be the other way round.

He is using the secondary index to do the FindKey, so the key violation is
presumably because of a clash on the Primary Key.

So, what is the primary key?

--Bill Sparrow--
Member of the UK Borland User Group

Re:I incorrectly get Key Violation!! A truly spurious Key Violation


Joseph <jgl...@paytotheorder.com> a crit dans le message :
3aad277d_2@dnews...

Quote
> I know you must be thinking that I'm doing something wrong, and normally
I'd
> be inclined to agree. However, in this case, I believe the db table has
> gotten into some state which cannot be repaired.

> It's a very small table, on which I have a secondary index with 3 fields.
I
> am trying to append to this table. One of the index fields in the new data
> record to be added has a value of 45. I can easily look through the 2
dozen
> records in the table to see that there is NO 45 in there.

> I execute the following code, and FindKey CORRECTLY does NOT find a
matching
> key.  There should NEVER be a Key Violation anyway, since the following
code
> will EDIT the record if a matching key is found!

> So, the code gets to the Post and produces a Key Violatin anyway, and the
> record can't be stored in the db!!

> Can anyone help solve this Case of the Spurious Key Violation??
Thanks!!!!

>   // Header is a TTable component, and H is a record structure containing
> data
>   Header.IndexName := 'HeaderInfo';
>   if FindKey([H.RunDate,H.Run,H.ServerName])
>     then Edit
>     else Append;
>   Header.FieldByName('Run Date').AsDateTime := H.RunDate;
>   Header.FieldByName('Run').AsInteger := H.Run;
>   Header.FieldByName('Server Name').AsString := H.ServerName;
>   Header.FieldByName('Download Timestamp').AsDateTime := H.DateTime;
>   Header.FieldByName('Software Version').AsString := H.Version;
>   Header.Post;   // <<<---  get a Key Violation here!!
>   result := Header.FieldByName('ID').AsInteger;

> P.S. I still get this Key Violation despite rebuilding the table and
> indexes. The only way I do NOT get a key violation is if I delete the
table
> entirely, and recreate the table. Then the new data goes in just fine. But
I
> need to keep the old data as well, so that alone is not a good solution.

> Besides, I'd like to find out what is causing this spurious Key Violation.
> But unless we find out, it will happen again!

I had this problem two weeks ago. The problem was that the BDE was seeing
only 80% of my records. So when I tried to append and add a record that was
there but not seen by the BDE, I got a key violation. I fixed it sorting my
table according to my primary indexes with the Database Module that come
with Delphi.

Hope this could help
Patrick Paris

Other Threads