Solved

Overriding the Dobeforepost procedure in Tquery Help!

Posted on 2014-01-06
21
728 Views
Last Modified: 2014-01-07
Hi can any one help I need to solve this

Query object inherited from TQuery which will only post a
record if it is currently in dsInsert or dsEdit state by overriding the
protected DoBeforePost procedure.

any ideas?
0
Comment
Question by:Remmie26
  • 11
  • 7
  • 2
21 Comments
 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39760687
No need to override anything.Just check dataset state and do post. I use before scroll event. You can use OnClose event of form too.
procedure TForm1.dataBeforeScroll(DataSet: TDataSet);
begin
   PostData(DataSet);
end;

procedure TForm1.PostData(DataSet: TDataSet);
begin
    if DataSet.Active and (DataSet.State in [dsEdit, dsInsert]) then
    begin      
      DataSet.Post;
    end;
end;

Open in new window

0
 

Author Comment

by:Remmie26
ID: 39760693
I know, what you are saying and i agree with you however, I have a requirement to create an object base on TQuery and override its doBeforepost procedure to not post a
record if it is currently in dsInsert or dsEdit
0
 

Author Comment

by:Remmie26
ID: 39760702
I tried creating a component and it didn't work

I also tried  

 TxQuery = class(TQuery)
  public
    procedure DoBeforePost; override;
  end;


procedure TxQuery.DoBeforePost;
var
  aallow : boolean;
begin

  aallow :=  TcpQuery(self).DataSource.State in [dsedit,dsinsert];
  if not aallow  then
     abort;
 inherited DoBeforePost;
end;

but its not working what am I doing wrong?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 26

Expert Comment

by:Sinisa Vuk
ID: 39761408
You want to do Post or not when you in Edit/Insert state? Why do you need this - to make dataset readonly?

Anyway - try this:

procedure TxQuery.DoBeforePost;
var
  bEditIns : boolean;
begin

  bEditIns :=  Self.State in [dsEdit,dsInsert];
  if bEditIns then
  begin
    Self.Cancel;
    Abort;
  end;
end;

Open in new window


Your code test if dataset is in edit/insert and if it is not then do Abort !?!
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761507
you want to edit/insert records and not post the changes to the database ?

Have you tried setting the following properties:
Query.CachedUpdates := True;

and in OnUpdateRecord event, reply with uaAbort

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet; UpdateKind: TUpdateKind;
  var UpdateAction: TUpdateAction);
begin
  UpdateAction := uaAbort;
end;

this way you can edit the dbgrid without posting changes to the database
0
 

Author Comment

by:Remmie26
ID: 39761512
??
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761523
a TQuery object only ever posts a record it is in dsEdit or dsInsert
otherwise the methode DoBeforePost doesn't get called

This the source for the Post method of TDataset:

procedure TDataSet.Post;
begin
  UpdateRecord;
  case State of
    dsEdit, dsInsert:
      begin
        DataEvent(deCheckBrowseMode, 0);
        DoBeforePost;
        CheckOperation(InternalPost, FOnPostError);

Open in new window


what are you trying to accomplish ?
0
 

Author Comment

by:Remmie26
ID: 39761526
code test to see if data set is in edit or insert mode and if not abort the post otherwise continue. It is a something I need to answer and I can can not understand the logic behind it by still need to answer.

also I need  the new DoBeforePost to override the Tdataset.dobeforepost (because is a subclass of Tquery). and unless I call it manually xquery1.dobeforepost it does not execute

help
0
 

Author Comment

by:Remmie26
ID: 39761532
Geert_Gruwez you are correct, I looked at the DB.pas as well to see the original dobeforepost and for the life of me I can not figure out why they want what they want,. but still i need to answer :).  how do I make sure the overriden procedure is the one that get executed?
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761533
that didn't make sense to me

DoBeforePost does not get called when the Dataset is not in Edit or Insert mode
you will never get that event called, so it's pointless overriding it

if i just open a query/datasource/grid on my form, i get this error:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Query1.Post;
end;

>> Error: Query1: Dataset not in edit or insert mode
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761541
you can catch the error if it is not in that mode:

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Query1.Post;
  except
    on E: Exception do
      ShowMessage(Format('class: %s, message: %s', [E.ClassName, E.Message]));
  end;
end;

>> this gives:
---------------------------
Project1
---------------------------
class: EDatabaseError, message: Query1: Dataset not in edit or insert mode
---------------------------
OK  
---------------------------
0
 

Author Comment

by:Remmie26
ID: 39761542
LOL what I thought and spend hours to get it to work with no luck. I did everything you did  just want to make sure I give the correct advice

BTW this is the original request

Write a TxQuery object inherited from TQuery which will only post a
record if it is currently in dsInsert or dsEdit state by overriding the
protected DoBeforePost procedure.
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 39761544
the answer is the following

DoBeforePost only gets called when the dataset is in dsEdit or dsInsert start
otherwise an error is raised
The  checks are done in the Post methode of the TDataset component

the one asking you the question is either new to Delphi, has no clue what the underlying objects do or wanted to send you on a wild goose chase
0
 

Author Comment

by:Remmie26
ID: 39761546
haha  I guess  "wanted to send you on a wild goose chase" thanks I thought i am going crazy for a second.
0
 

Author Comment

by:Remmie26
ID: 39761571
I've requested that this question be closed as follows:

Accepted answer: 0 points for Remmie26's comment #a39761546

for the following reason:

this it the answer i was seeking
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761548
it was somewhat longer than a second by my reckoning :)
0
 

Author Closing Comment

by:Remmie26
ID: 39761572
Thanks
0
 

Author Comment

by:Remmie26
ID: 39761674
Geert Gruwez what wrong with the way I closed the question?
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 39761678
you accepted your own answer #a39761546 at first which only indicated someone maybe being crazy but didn't have any solution to the question

now it's ok

it's possible to accept multiple answers also if multiple answers helped.
0
 

Author Comment

by:Remmie26
ID: 39761689
LOL I guess I am going crazy after all
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi TcxGrid group footer summary 3 272
tvirtualstringtree freeze when load too manny images 10 63
Base1 Encode/Decode 3 77
Wincontrol not (correctly) drawn 15 38
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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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