?
Solved

Working with CacheUpdate !!

Posted on 1998-05-31
1
Medium Priority
?
489 Views
Last Modified: 2010-04-03
Hi,
I am working in delphi 3.0 using  'Paradox' ..I have two tables which are order_master and order_detail and i have a form in which both have master detail relationship i mean its a master detail form using the above tables..and cahcheupdate property is true in both tables.

So in case of keyviolation in detail table I want to trap this keyviolation on order_detail  but you know when cahcheupdate is true then it wont work coz changes are going in a temporary area...not physically...so is there any method of preventing duplicate values in detail when cacheupdate is true...

your help in this matter will higly appreciated...

thanks,
Zahid

0
Comment
Question by:zahzia
[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
1 Comment
 
LVL 1

Accepted Solution

by:
Edo082297 earned 40 total points
ID: 1349438
Hi Zahid,
   There is no magic solution to predicting or preventing key violations. However, you can deal with them in a more sophisticated manner than the default behavior provided by Delphi.
  You need to use transactions. A transaction applies updates in an all or nothing fashion. There are two solutions to providing transaction control with cached updates. One is to provide a transaction wrapper around your call to a DataSet's ApplyUpdates method. The second is to use the ApplyUpdates method of the DataSet's Database property (what we'll do). The database property is just a pointer to a Database object you put on your form (which you could do) or the database object that you reference (Delphi will create one for you automatically). Note that since you are using Paradox local tables, you must set the transisolation property of the database object to tiDirtyRead in your code. (This just means that the records will be treated as if the records are being automatically committed, even though a transaction might get rolled back). Note that you do not need to put a database object on your form for all this to work! If you would prefer to do so, drop one on the form and set your datasets to point to it through their datasource property.
   The database object provides transaction processing. It is the way that we will implement a solution. If you look at the following code, you will notice commit being called prior to commitupdate. The purpose of committing a transaction prior to committing the updates is that you do not want to commit the updates (which flushes the cache) if the committing of the transaction fails (due to a key violation, or a data constraint, etc...).

Table1.Database.StartTransaction;
try
  Table1.ApplyUpdates;
  Table1.Database.Commit;
  Table1.CommitUpdates;
except
  Table1.Database.Rollback;
  raise;
end;

   You cannot prevent the user from trying to insert a record that will cause a key violation. However, by providing a handler for the OnUpdateError, you can do some other processing, skip over the offender, or allow the user to insert another value. An exception type is part of this events signature so that you know what went wrong.
  In this example, we catch key violations and allow the user to insert another value before attempting to update the record again:

procedure TForm1.Table1UpdateError(DataSet: TDataSet; E: EDatabaseError; UpdateKind: TUpdateKind; var UpdateAction: TUpdateAction);
Var
  BdeError: PChar;  NewVal: String;
begin
  GetMem(BdeError, 1024);
  try
    BDE.DbiGetErrorString(DBIERR_KEYVIOL,BdeError);
    //Test if a key violation message is in the exception.
    //If a key violation occurred, permit the user to enter a new key
    if E.message = copy(StrPas(BdeError),1,Length(E.message)) then  
    begin
      NewVal := DataSet.Fields[0].NewValue;
      if InputQuery('Key violation','Enter new key',NewVal) then
      begin
        DataSet.Edit;
        DataSet.Fields[0].Value := NewVal;
        DataSet.Fields[0].NewValue := NewVal;
        UpdateAction := uaRetry;
      end
    else
      //Use uaAbort since the user has clicked Cancel.
      //No error message is necessary.
      UpdateAction := uaAbort;
  end
else
  ;//The default UpdateAction is uaFail.
   //Permit the update to fail.
finally
  FreeMem(BdeError);
end;

end;

  This event handler includes a case statement to test the value of UpdateKind. If the current record is being inserted, a new record is added to the table, the fields of the Query are assigned to the fields of the table, and then the record is posted. If the record has been modified, the existing record is located in the table, and then its fields are updated. If the record has been deleted, again the corresponding record is located in the table and deleted. If the actions of this event handler do not raise an exception, the final statement sets the UpdateAction parameter to uaApplied. If an exception is raised, the UpdateAction's default value of uaFail remains unchanged, causing the update to fail.
  It is not the greatest idea to let the user make changes while transactions are occurring, but it may suit your needs. Make sure to thoroughly test a handler such as this.
  There is an awful lot to cached updates; read up on this event (OnUpdateError) and the OnUpdateRecord event which allows a great deal of customization to the way transactions are implemented.

HTH

Edo

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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