Recent

Author Topic: What is the proper way to save a new record?  (Read 3727 times)

dodgebros

  • Full Member
  • ***
  • Posts: 161
What is the proper way to save a new record?
« on: April 19, 2015, 03:22:10 am »
I have a datamodule with a IBConnection component, a SQLQuery component, a SQLTransaction component, and a Datasource component on it.

I have a form with some TEdits, a TDBCombobox and a DBGrid and a button to save new records with.  FYI, I only used two of the fields to just do testing and to learn on for now. Here is the code under the Save button:

Code: [Select]
procedure TfrmMain.btnCommitClick(Sender: TObject);
begin
  with DataModule1.SQLQry_TimeSheets do
    begin
      Insert;
      FieldByName('pid').AsInteger := 10;
      FieldByName('client').AsString := DBcboClients.Text;
      Post;
      ApplyUpdates;
      DataModule1.SQLTrans_TimeSheets.Commit;
      DBGrid1.Refresh;
      //Close;
      //Open;
    end;
end;   

I have several questions regarding my code:

1) Is the "Post" needed?

2) Just using DBGrid1.ReFresh does not cause the DBGrid to show the new record.  However, eliminating the DBGrid1.ReFresh and enabling the "Close" and "Open" do cause the DBGrid to show the new record.  Even thought this works, is this the proper way to code this?

3)Finally, are there more proper ways to do all of this?  I know I could use TDBEdits but I have my reason for just using TEdits.

Just trying to learn to code properly ....

Thanks,
TD

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: What is the proper way to save a new record?
« Reply #1 on: April 19, 2015, 03:43:42 am »
1) Post instructs the dataset that editing process is done and the new data are ready to be persisted so yes it is needed. ApplyUpdates collects all the new data and persist them to the database what every this database might be.

2) That explains absolutely nothing, where is the dbgrid1 linked against? if it is linked against the SQLQry_TimeSheets then even after the post it should show the new with out any refresh calls if it is not then the dataset it is linked against needs to re read from the database after the applyupdate call in order to get the new data.

3) DBEdit is a marvelous component it helps a lot when used properly in the correct data entry but there is no such thing as a proper way to handle data, so anything goes. Make sure that your posts/applyupdate/commit are in the correct order and time to make sure you will not loose any data and everything else is considered proper.

Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

dodgebros

  • Full Member
  • ***
  • Posts: 161
Re: What is the proper way to save a new record?
« Reply #2 on: April 19, 2015, 08:07:52 pm »
Thanks taazz for trying to help.

2) The Datasource property of DBGrid1 is set to DataModule1.DS_TimeSheets which has it's Dataset property set to DataModule1.SQLQry_TimeSheets.  DataModule1.SQLQry_TimeSheets has it's Database property set to IBConnection1 and it's Datasource property is left empty.   The only other piece is the SQLTransaction component, DataModule1.SQLTrans_TimeSheets which has it's Database property set to IBConnection1.

Here is a visual of the linking:
IBConnection1-->SQLQry_TimeSheets-->DS_TimeSheets-->DbGrid1
IBConnection1-->SQLTrans_TimeSheets   

I assume this is the correct way to connect these components.

As for the DBGrid needing to be refreshed in order to see the new record after it is added, I tried this code and what happens after you click the Save button (btnCommit) is the DBGrid goes blank.  So, now what?

Code: [Select]
procedure TfrmMain.btnCommitClick(Sender: TObject);
begin
  with DataModule1.SQLQry_TimeSheets do
    begin
      Insert;
      FieldByName('pid').AsInteger := 6;
      FieldByName('client').AsString := DBcboClients.Text;
      Post;
      ApplyUpdates;
      DataModule1.SQLTrans_TimeSheets.Commit;
      //DBGrid1.Refresh;
      //Close;
      //Open;
    end;
end; 

Thanks,
TD                 

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: What is the proper way to save a new record?
« Reply #3 on: April 19, 2015, 08:52:13 pm »
Code: [Select]
      DataModule1.SQLTrans_TimeSheets.Commit;
      //DBGrid1.Refresh;
      //Close;
      //Open;
My guess... you need to do a CommitRetaining.
Code: [Select]
      DataModule1.SQLTrans_TimeSheets.CommitRetaining;
When doing a .Commit the transaction will be closed and no transaction will be active, resulting in a blank DBGrid.

dodgebros

  • Full Member
  • ***
  • Posts: 161
Re: What is the proper way to save a new record?
« Reply #4 on: April 19, 2015, 10:34:32 pm »
That worked!

Thanks rvk,
TD

 

TinyPortal © 2005-2018