Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Catching Key Violations

Posted on 1997-07-03
8
Medium Priority
?
1,261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 5

Accepted Solution

by:
ronit051397 earned 160 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

719 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