Solved

How long does a TDataset bookmark remain valid?

Posted on 2009-05-14
3
1,016 Views
Last Modified: 2013-11-23
I have code like below in a project I'm working.

Personally, I do not use bookmarks much  except to reposition a dataset where I only moved the cursor position (to create a listing, fill a string list, etc). If I Refresh, update (especially when a filter can make the record invisible), refetch (Close/Open) or any operation that modifies the data in the dataset, I don't use bookmarks. I prefer to Locate on the primary key (using a TClientDataset, of course) or requery modifying the parameters.

Until when is a bookmark valid? Until a Refresh? Until a Close/Open is done to refetch data? Where does the safe zone end?

Consider in the answer I'm using TClientDataset with a TSQLQuery (DbExpress).
procedure TForm.EditBtnClick(Sender:TObject);
begin
  // Mark is form variable. It's private
  Mark = cdsMain.GetBookmark;
  // blabalbal
  .
  .   
  .
end;
 
procedure TForm.OkBtnClick(Sender:TObject);
var  
  mistakes: Integer;
begin
  //Validation stuff and transaction control
  //removed to not clutter the code
  If cdsMain.ChangeCount <> 0 then 
    mistakes := cdsMain.AppyUpdates(-1); 
  cdsMain.Refresh;
  try
    cdsMain.GotoBookmark(Mark);
    // Yes, I know I would have to call FreeBookmark
    // but I'm just reproducing 
  except
    cdsMain.First;
  end;
end;

Open in new window

0
Comment
Question by:Delphian
3 Comments
 
LVL 26

Accepted Solution

by:
EddieShipman earned 250 total points
ID: 24390400
Compiled from two posts on the CodeGear forums:

You can not retain or reuse TBookMarks once a TDataSet has been closed and reopened. A TBookmark is a reference
to a record pointer not the data contained within the record. TBookmark is not guaranteed to be valid after the
dataset is closed in any case (IBX, BDE or other dataset). It is documented this way. The only way to do
this is reliably is to store off a key value that you can re lookup the record in the new result set.
0
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 200 total points
ID: 24403900
A bookmark is actually just a offset to the n-th record.
if you have 1 recordset, take a bookmark and use that on a other (same size record) recordset, you'll end up on the same n-th record.

so it's valid as long as dataset.BookmarkValid(Bookie) = True

as EddieShipman says ... use the primary key
0
 
LVL 4

Author Comment

by:Delphian
ID: 24424968
Interesting results. Have you tried that specifically with TClientDatasets??
After a little research on Db.pas, I saw that all *Bookmark methods are abstract,
so the descendants are responsible for the implementation.


0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

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 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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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