Recent

Author Topic: I encountered onDatachanged issue with TDatasource  (Read 934 times)

Fantablup

  • Full Member
  • ***
  • Posts: 171
I encountered onDatachanged issue with TDatasource
« on: March 26, 2024, 10:31:16 pm »
When i use disableControls and enableControls,
When i re-enable with enableControls on a dataset, the onDataChanged is executing again.

I also tried this without doing anything between disable and enable. It is the same.

Like this:
procedure TfrmMarket.dtsCountiesDataChange(Sender: TObject; Field: TField);
begin 
SQLCounties.DisableControls;
SQLCounties.EnableControls; // This re-execute the DataChange
end;


So, i had to call a procedure from afterScroll in the dataset that do the stuff in the  instead.  This works good.

Fantablup

  • Full Member
  • ***
  • Posts: 171
Re: I encountered onDatachanged issue with TDatasource
« Reply #1 on: March 27, 2024, 04:01:57 am »
It seems like this is kind of buggy when using a dataset with mastersource and masterfields.
Tried to also use this with countries, counties and cities. Couldn't get it to work either. Not when using tcombobox on each and populate them with a array of records.
Since it always re-execute ondatachange when using enablecontrols in the ondatachange function, it is not working.

I tried to use global variables to stop the second execution, but then other things is faulty.

My solution is to remove all mastersource and masterfields, and instead use onchange on each tcombobox and rerieving the array of record index, closing the dataset, changing the parameter on it, and re-open it.

When it is first opened, it execute the updating function.

This is also what i do in any other language, and it just works.

These are the things i have encountered till now when using Lazarus.


rvk

  • Hero Member
  • *****
  • Posts: 6201
Re: I encountered onDatachanged issue with TDatasource
« Reply #2 on: March 27, 2024, 07:28:56 am »
It seems like this is kind of buggy when using a dataset with mastersource and masterfields.
The problem here is that you are messing with events in a deep level, not knowing when they actually fire.

Imagine this...
1) TSQLQuery.EnableControl will check a flag to see if the data has changed. If it has it needs to call TDataSource.DataChange.
2) AFTER TDataSource.DataChange... the flag is cleared.

In TDataSource.DataChange... you toggle TSQLQuery.DisableControl and TSQLQuery.EnableControl.
Because the previous flag hasn't cleared yet (because it's still in DataChange)... the TDataSource.DataChange is triggered again in TSQLQuery.EnableControl (see rule #1).

I agree... this could be handled internally better.. but what if you really change the data between DisableControl and EnableControl there ???
Then it should re-fire (because the data has changed.

Small question... WHAT are you actually using the DisableControl and EnableControl for? What are you doing in between those in DataChange?

BTW. Using a global flag should have worked if you used it correctly.
At least... this works for me...

Code: Pascal  [Select][+][-]
  1. var
  2.   NotOkToEnterDataChange: boolean = False;
  3.  
  4. procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
  5. begin
  6.   if NotOkToEnterDataChange then exit;
  7.   Caption := Caption + '.';
  8.   NotOkToEnterDataChange := True;
  9.   try
  10.     SQLQuery1.DisableControls;
  11.     // ...
  12.   finally
  13.     SQLQuery1.EnableControls;
  14.     NotOkToEnterDataChange := False;
  15.   end;
  16. end;


Fantablup

  • Full Member
  • ***
  • Posts: 171
Re: I encountered onDatachanged issue with TDatasource
« Reply #3 on: March 27, 2024, 01:30:34 pm »
Japp, that exactly what i did too rvk.

It works with this when it is not as master detail.
But using it as master detail from countries it did not work, i tried also a global var on the country database and tried other stuff, but no go.

The solution i came up with was better. no need for ondatachange. Only using onchange on the tcombobox instead.

I use this for looping through all records and populating tcombox. No change in any date here.

rvk

  • Hero Member
  • *****
  • Posts: 6201
Re: I encountered onDatachanged issue with TDatasource
« Reply #4 on: March 27, 2024, 01:42:35 pm »
I use this for looping through all records and populating tcombox. No change in any date here.
Ha, yes. But using EnableControls in OnDataChange is problematic (as you have seen) because of it's changed flag still being set.

I don't think many discover this because normally you would fill the combobox at a different event.
for example in onexit or onscroll, or like you do now, in TWinControl.OnChange.


Fantablup

  • Full Member
  • ***
  • Posts: 171
Re: I encountered onDatachanged issue with TDatasource
« Reply #5 on: March 27, 2024, 02:34:08 pm »
Hehe :)

Yes, i saw that :)

And, yes, most would not do this with a tcombobox. But i needed to do it :) I tried to use this while automatically getting data from the master/detail.
Normally it is not used this way.

And, yes, it became very problematic, as you said.
But hey, We learn by doing, and making things work by solutions :)

 

TinyPortal © 2005-2018