Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 8 of 8 total
Thread show autinc in grif after insert
Tue, Sep 9 2014 9:56 AMPermanent Link

Harry de Boer

Hi

I use a dbisam table with an autoinc field. In a grid the data is shown. I have a button onclick with

Database.StartTransaction;
dbisam.Insert ;
dbisam.Save ;
Database.Commit;

because I thought that way it would show the incremented value in the gridcolumn.

This does not work however. What's the proper way to show an autoinc after an insert right away?

Regards, Harry
Tue, Sep 9 2014 2:31 PMPermanent Link

Walter Matte

Tactical Business Corporation

Harry de Boer wrote:

Hi

I use a dbisam table with an autoinc field. In a grid the data is shown. I have a button onclick with

Database.StartTransaction;
dbisam.Insert ;
dbisam.Save ;
Database.Commit;

because I thought that way it would show the incremented value in the gridcolumn.

This does not work however. What's the proper way to show an autoinc after an insert right away?

Regards, Harry
Tue, Sep 9 2014 3:09 PMPermanent Link

Walter Matte

Tactical Business Corporation


You would need load, after the commit, so see the number that was assigned.  Commit sends it to the Server to Insert, only.

In the AfterCommit event add:

Database.Load(dbisam);
Tue, Sep 9 2014 3:33 PMPermanent Link

Harry de Boer

Walter Matte wrote: You would need load, after the commit, so see the number that was assigned.  Commit sends it to the Server to Insert, only. In the AfterCommit event add: Database.Load(dbisam);

Hi Walter,

If you use Database.Load(dbisam) it seems to me that the whole dataset is loaded again. Is this correct? Seems not the most efficient way then (don't know for sure though, perhaps only data is loaded that is visible in the grid?). Furthermore, you lose focus on the inserted row if you do that (focus is on the first record in the grid). Is there a way to refresh the grid(row), or another efficient way.

Regards, Harry  
Tue, Sep 9 2014 4:10 PMPermanent Link

Walter Matte

Tactical Business Corporation

Hi Harry:

I'm not advocating this as a solution.

Especially if you are dealing with a Master/Detail data relationship you will want to do things differently.

What I have done is perform a ServerRequest to a function I wrote to GetTheNextId for a Table.  I have a Table "NextID" with 2 fields TableName and NextID,  which the function GetTheNextID updates,  and NOT rely on AutoInc, because it not a good solution in environment.

In the ServerRequest OnComplete event Insert into the Database and assign the number that was returned.

You end up not using some id's when the user cancels the insert, but that is fine with me.

this mechanism allows you to do a bunch of Insert/Update and Deletes which are all taken care of upon Commit.

Walter


Harry de Boer wrote:

Walter Matte wrote: You would need load, after the commit, so see the number that was assigned.  Commit sends it to the Server to Insert, only. In the AfterCommit event add: Database.Load(dbisam);

Hi Walter,

If you use Database.Load(dbisam) it seems to me that the whole dataset is loaded again. Is this correct? Seems not the most efficient way then (don't know for sure though, perhaps only data is loaded that is visible in the grid?). Furthermore, you lose focus on the inserted row if you do that (focus is on the first record in the grid). Is there a way to refresh the grid(row), or another efficient way.

Regards, Harry  
Wed, Sep 10 2014 3:39 PMPermanent Link

Ronald

I do not use autoincrement fields. I generate my own unique id with the
following function. It is not lightning fast of course, but I think the
chance that it generates the same is very slim and it saves me the hassle
with the autoinc field.

function GetUUID:string;
begin
Result:=Pad(IntToStr(DayOf(Date)),2,'0')+
           Pad(IntToStr(MonthOf(Date)),2,'0')+
           Pad(IntToStr(YearOf(Date)),4,'0')+
           Pad(IntToStr(HourOf(Time)),2,'0')+
           Pad(IntToStr(MinuteOf(Time)),2,'0')+
           Pad(IntToStr(SecondOf(Time)),2,'0')+
           Pad(IntToStr(MSecondOf(Time)),4,'0')+
           Pad(IntToStr(Round(Random*1000)),4,'0')+
           Pad(IntToStr(Round(Random*1000)),4,'0')+
           Pad(IntToStr(Round(Random*1000)),4,'0');
end;

Ronald

"Walter Matte" schreef in bericht
news:980083B1-FEF9-4FD9-B6FB-5A35EDB82DD5@news.elevatesoft.com...

Hi Harry:

I'm not advocating this as a solution.

Especially if you are dealing with a Master/Detail data relationship you
will want to do things differently.

What I have done is perform a ServerRequest to a function I wrote to
GetTheNextId for a Table.  I have a Table "NextID" with 2 fields TableName
and NextID,  which the function GetTheNextID updates,  and NOT rely on
AutoInc, because it not a good solution in environment.

In the ServerRequest OnComplete event Insert into the Database and assign
the number that was returned.

You end up not using some id's when the user cancels the insert, but that is
fine with me.

this mechanism allows you to do a bunch of Insert/Update and Deletes which
are all taken care of upon Commit.

Walter


Harry de Boer wrote:

Walter Matte wrote: You would need load, after the commit, so see the number
that was assigned.  Commit sends it to the Server to Insert, only. In the
AfterCommit event add: Database.Load(dbisam);

Hi Walter,

If you use Database.Load(dbisam) it seems to me that the whole dataset is
loaded again. Is this correct? Seems not the most efficient way then (don't
know for sure though, perhaps only data is loaded that is visible in the
grid?). Furthermore, you lose focus on the inserted row if you do that
(focus is on the first record in the grid). Is there a way to refresh the
grid(row), or another efficient way.

Regards, Harry
Thu, Sep 11 2014 4:46 AMPermanent Link

Matthew Jones

Ronald wrote:

> I do not use autoincrement fields. I generate my own unique id with
> the following function. It is not lightning fast of course, but I
> think the chance that it generates the same is very slim and it saves
> me the hassle with the autoinc field.

In my "unique" IDs, I tend to add a counter too, so you can't get the
same ID even due to randomness. Depends how often you call it of course
- if it is more than a millisecond between calls, you have that already.

--

Matthew Jones
Fri, Sep 12 2014 4:42 AMPermanent Link

Harry de Boer

Thanks Matthew, Ronald for your input,

I got it running now:  

implementation

const
  port = '88';

procedure TfrmDataExample.frmDataExampleCreate(Sender: TObject);
begin
  Database.Load(dbisam1);
  LogOutput('Data loaded','http://localhost:'+ port +'/log');
end;

procedure TfrmDataExample.btnInsertClick(Sender: TObject);
begin
  if not Database.InTransaction then Database.StartTransaction;
  dbisam1.Insert(true);
  btnSaveClick(sender);
  btnEdit.SetFocus;
end;

procedure TfrmDataExample.dbisam1InitRow(Sender: TObject);
begin
  dbisam1.Columns['id'].AsString := createUUID;
end;

procedure TfrmDataExample.btnSaveClick(Sender: TObject);
begin
  try
     dbisam1.Save;
     Database.Commit;
  except
     MessageDlg('Not in Edit mode!','Record not saved',mtWarning,[mbOk]);
  end;
  gridData.Rowselect := True;
  lblEditMode.Caption := '';
end;

procedure TfrmDataExample.btnEditClick(Sender: TObject);
begin
  gridData.Rowselect := False;
  if not Database.InTransaction then Database.StartTransaction;
  if not dbisam1.State = dsUpdate then dbisam1.Update;
  lblEditMode.caption := '[Edit mode]';
end;

function TfrmDataExample.createUUID:string;
begin
  Result:=Pad(IntToStr(DayOf(Date)),2,'0')+
          Pad(IntToStr(MonthOf(Date)),2,'0')+
          Pad(IntToStr(YearOf(Date)),4,'0')+
          Pad(IntToStr(HourOf(Time)),2,'0')+
          Pad(IntToStr(MinuteOf(Time)),2,'0')+
          Pad(IntToStr(SecondOf(Time)),2,'0')+
          Pad(IntToStr(MSecondOf(Time)),4,'0')+
          Pad(IntToStr(Round(Random*1000)),4,'0')+
          Pad(IntToStr(Round(Random*1000)),4,'0')+
          Pad(IntToStr(Round(Random*1000)),4,'0');
end;

procedure TfrmDataExample.frmDataExampleShow(Sender: TObject);
begin
  btnEdit.SetFocus;
end;

Regards, Harry
Image