Solved

Catching Key Violations

Posted on 1997-07-03
8
1,235 Views
Last Modified: 2008-02-26
I'm trying to write code that cathes key violations in a Paradox table's composite key (the key is made up of two fields).  What code should i use to 1) catch the violation before it happens (maybe onBeforePost or onPostError?) and 2) cancel the record.
0
Comment
Question by:d4jaj1
  • 4
  • 4
8 Comments
 
LVL 5

Accepted Solution

by:
ronit051397 earned 40 total points
ID: 1338156
Usually you catch key violation on the OnBeforePost event.
In there write a code that checks key violation or use try, if there is a key violation, than cancel posting by using the 'Cancel' procedure or the 'Abort' procedure.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338157
Okay, then why am I having problems with the below code?

procedure TfrmActivity.tblActivityBeforePost(DataSet: TDataSet);
begin
  try
    tblActivity.Post;
  except
     on E: EDBEngineError do begin
       if(E.ErrorCount > 0) And
        (E.Errors[0].ErrorCode = DBIERR_KEYVIOL) then
           ShowMessage('You''ve inserted the duplicate record');
    end;
end;
end;

I get an exception on the Key Violation stating "EStackOverflow  with message 'StackOverflow'"  What does this mean and what am I doing wrong in my error code?
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338158
Don't use Post on the event 'beforePost', becuese you are creating a kind of "Recursion". Use Post on other events.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338159
When I take of the line that contains 'Post', doesn't get to the EXECPT block and Delphi stops, probably because the code I have doesn't cath the key violation.  When I pasted the code below to the onPostError, it still wouldn't work.  Do you know the correct code to catch the key violation?

If  E: EDBEngineError then
  if(E.ErrorCount > 0) And (E.Errors[0].ErrorCode = DBIERR_KEYVIOL) then
    table1.cancel;
    ShowMessage('You''ve inserted the duplicate record');
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:ronit051397
ID: 1338160
procedure TForm1.Table1PostError(DataSet: TDataSet; E: EDatabaseError;
  var Action: TDataAction);
begin
  if E.message = 'Key violation.' then Action:=daAbort; //cancel posting
end;

commets:
1. This works for delphi3. I think in delphi2 you should write
'Key Violation' without the dot in the end
2. Set Integrated Debugging to False.

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338161
Excellent, that works!  However, I have other 2 questions (that's why I raised the points.

1.  Why wouldn't this work with the Integrated Debugger ON?  If I cut it off, doesn't that mean Delphi woun't tell me if other errors occur?  Futhermore, the code I entered is in the onPostError - shouldn't that be a reason for Delphi not to stop on its own - because I wrote an exception handler?
2.   My key field is called Name.  A value 'ANIS' already exists.  If I enter 'anis' in lowercase, no keyviolation occurs.  Why and how can I stop this?  I'm using the BDE and Paradox tables.
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1338162
Answer 1:
The reason you need to set off the integrated debugging is because the abort action is a kind of silence exception.
Here is an explanation from Delphi help:

Delphi applications handle most exceptions that your code doesn't specifically handle by displaying a message box that shows the message string from the exception object. You can also define "silent" exceptions that do not, by default, cause the application to show the error message.
Silent exceptions are useful when you don't intend to handle an exception, but you want to abort an operation. Aborting an operation is similar to using the Break or Exit procedures to break out of a block, but can break out of several nested levels of blocks.

Silent exceptions all descend from the standard exception type EAbort. The default exception handler for Delphi applications displays the error-message dialog box for all exceptions that reach it except those descended from EAbort.
There is a shortcut for raising silent exceptions. Instead of manually constructing the object, you can call the Abort procedure. Abort automatically raises an EAbort exception, which will break out of the current operation without displaying an error message.

Example

The following code shows a simple example of aborting an operation. On a form containing an empty list box and a button, attach the following code to the button's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var  I: Integer;
begin
 for I := 1 to 10 do      { loop ten times }  begin
   ListBox1.Items.Add(IntToStr(I));      { add a numeral to the list }
   if I = 7 then Abort;      { abort after the seventh one }
 end;
end;

try to use the Cancel maethod instead of daAbort, maybe it will work.

Answer 2:
Yes. I am aware of this problem. In my oppinion, you should check this matter before posting a record and by useing this method:

if not Table1.Locate('Name', 'ANIS',[loCaseInsensitive]) then Table1.Post;

The Locate method will locate also 'anis', 'aNiS' etc.

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1338163
That's the best answer I've received from this site.  Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now