Board index » delphi » Handling Key Violations

Handling Key Violations

I know there must be a good example somewhere but I can't find a simple
procedure for the OnPostError event to detect if it is a key violation.
I want to delete the record if the dataset is in insert mode or cancel
the changes if it is not.

How do I detect if the OnPostError was caused by a key violation?  I
know the error is " $2601 (9729) = Key violation."
I don't know how to get the information from E : EDatabaseError.  Do I
have to check to see if E.message = 'Key violation'?  Although this
might work it would seem to be a bit shaky if you were in a
multi-lingual environment and be prone to problems based on the message
being changed sometime in the future.  Is there any easy way to get the
code value (9729)?

TIA ...Jim

 

Re:Handling Key Violations


Quote
Jim Andrews <j...@azdogs.com> wrote:
>I know there must be a good example somewhere but I can't find a simple
>procedure for the OnPostError event to detect if it is a key violation.
>I want to delete the record if the dataset is in insert mode or cancel
>the changes if it is not.

>How do I detect if the OnPostError was caused by a key violation?  I
>know the error is " $2601 (9729) = Key violation."
>I don't know how to get the information from E : EDatabaseError.  Do I
>have to check to see if E.message = 'Key violation'?  Although this
>might work it would seem to be a bit shaky if you were in a
>multi-lingual environment and be prone to problems based on the message
>being changed sometime in the future.  Is there any easy way to get the
>code value (9729)?

Taken from the DM1.PAS in the Borland/Delphi5/Demos/DB/DBErrors/
directory:

"A complete listing of the database errorcodes is found in the
DBIErrs.Int file in the Delphi/Doc directory or in the IDAPI.h
file in the Borland Database Engine."

Smola
--
"And once again, the day is saved!"
(supersmola.tripod.com/index.htm}

Re:Handling Key Violations


Quote
On Sat, 21 Oct 2000 01:22:44 -0700, Jim Andrews <j...@azdogs.com> wrote:
>I know there must be a good example somewhere but I can't find a simple
>procedure for the OnPostError event to detect if it is a key violation.
>I want to delete the record if the dataset is in insert mode or cancel
>the changes if it is not.

>How do I detect if the OnPostError was caused by a key violation?  I
>know the error is " $2601 (9729) = Key violation."
>I don't know how to get the information from E : EDatabaseError.  Do I
>have to check to see if E.message = 'Key violation'?  Although this
>might work it would seem to be a bit shaky if you were in a
>multi-lingual environment and be prone to problems based on the message
>being changed sometime in the future.  Is there any easy way to get the
>code value (9729)?

procedure TSomething.TDataSet1PostError(DataSet: TDataSet;
  E: EDatabaseError; var Action: TDataAction);
begin
  if (E is EDBEngineError)
      and (EDBEngineError(E).Errors[0].ErrorCode = DBIERR_KEYVIOL) then begin
    // There you go....
  end;
end;

--
The children were playing 'Other Kind of People', and I asked them what
it meant.  "We play we're the Other Kind of People, and nobody can tell
we're not regular people," they said.  "They can't even guess it."

Re:Handling Key Violations


Take a look in the Delphi instalation folder, find the
Demos\Db\Dberrors\dberrors.dpr Project. There, you will find a good example
about it!

"Jim Andrews" <j...@azdogs.com> escreveu na mensagem
news:39F15254.7C8E115@azdogs.com...

Quote
> I know there must be a good example somewhere but I can't find a simple
> procedure for the OnPostError event to detect if it is a key violation.
> I want to delete the record if the dataset is in insert mode or cancel
> the changes if it is not.

> How do I detect if the OnPostError was caused by a key violation?  I
> know the error is " $2601 (9729) = Key violation."
> I don't know how to get the information from E : EDatabaseError.  Do I
> have to check to see if E.message = 'Key violation'?  Although this
> might work it would seem to be a bit shaky if you were in a
> multi-lingual environment and be prone to problems based on the message
> being changed sometime in the future.  Is there any easy way to get the
> code value (9729)?

> TIA ...Jim

Other Threads