Solved

Catching Key Violations

Posted on 1997-07-03
8
1,234 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

18 Experts available now in Live!

Get 1:1 Help Now