Createdatasource as shared datasource on forms

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

Createdatasource as shared datasource on forms

Postby developers10 » Thu Feb 23, 2017 5:37 pm

Hi,

I have a dynamically generated dataset from which i create a datasource using the dataset.createDataSource() method
(no way to know on forehand which fields the dataset will contain)

Then, I assign this datasource to two seperate forms, no problem.
On the first form :
Code: Select all
form.dataSource = dataset.createDataSource()

and for the second form :
Code: Select all
solutionModel.getForm('second_form").dataSource = solutionModel.getForm('first_form).dataSource;


But when performing a foundset.newRecord() in one form, the other one's foundSet is not updated.
How can I make sure that both forms use the same datasource/foundset ?
Thx
developers10
 
Posts: 31
Joined: Thu Feb 23, 2017 5:33 pm

Re: Createdatasource as shared datasource on forms

Postby achiary » Mon Feb 27, 2017 6:42 pm

First of all you have to understand that dataset and foundset are different things, although both can become a datasource :

1) Dataset is a temporary table that is erased at the end of the Servoy session. The information loaded in a dataset can be related to a single table, to multiple tables ( complex query with joins, count(*),sum(),etc.) or no relation al all with database contents.

Dataset manipulation has its own funcions : addRow, addColumn, getValue, set,Value, etc.; so you should not use .newRecord to add a row in a dataset.

A dataset can become a datasource associated to a form but any interactive update of the information is NOT going to any database table.

2) Foundset is a set of records of one table in a database.

In your case I do not know what kind of information holds the dataset, but if you are trying to use .newRecord probably you want to connect the form to a table; if this is the case I suggest the following :

- do not use dataset , just connect the form to the table in both forms , by default the foundsets are the same.

- use solution model to select which columns of the table (and of related tables) will appear in the form.

Hope this helps.
achiary
 
Posts: 83
Joined: Wed Nov 14, 2007 3:29 pm
Location: Argentina

Re: Createdatasource as shared datasource on forms

Postby patrick » Mon Feb 27, 2017 7:05 pm

I think the problem is not really related to whether the form is based on a database table or an in-memory table, but what foundsets you have in the forms. Usually, two forms share the same foundset if nothing interferes (like manually loading certain records, a relation loaded in one of the forms etc.). If something "interferes", you end up with two forms having two independent foundsets.

Not sure what other code is running here, but if you want to make sure two forms really share the same foundset you would have to do something like

Code: Select all
forms.a.controller.loadRecords(forms.b.foundset)


This tells form "a" to load the foundset of form "b" resulting in the two forms sharing the same foundset. Then you should see a new record in the other form as well.
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: Createdatasource as shared datasource on forms

Postby developers10 » Fri Mar 03, 2017 9:43 am

Hi achiary, patrick,

The dataset is created at runtime using a SQL pivot-instruction, so there really is no way to know on beforehand which columns the dataset will contain.
Our customer can define multiple of such rule sets and freely use columns of different tables.
I just put them horizontally on the same record using the Pivot instruction.

But as you say, the goal is indeed to create a dataset first and then create a common datasource for the forms using that dataset
Code: Select all
dataset.createDataSource()


In the meanwhile, I found out that new records do get created in all foundsets of the forms which share the same dataSource.
Except, when the form to which I assign this new datasource was 'in use' but not visible. Then, you still can assign this new dataSource without a problem, but it does not follow the manipulation
of a foundset on another form sharign the same datasource.

My problem apparently, consists of creating and assigning the datasource in runtime to a form in use and that is a no-no as far as I can tell.
That is, I can perfectly assign the new dataSource to that form, but it doesn't follow the manipulation of the foundset of another form.

But patrick's solution helps me if i put his code in the events triggered when adding a new record. This way, I can 'tell' the form in use which does not follow to copy one's form foundset.
I'll see to get this working.
Thank you both
Chris
developers10
 
Posts: 31
Joined: Thu Feb 23, 2017 5:33 pm

Re: Createdatasource as shared datasource on forms

Postby Andrei Costescu » Mon Mar 06, 2017 9:44 am

developers10 wrote:Except, when the form to which I assign this new datasource was 'in use' but not visible.

If that form is not visible you should be able to destroy it with history.remove('myForm'). Then if you alter it's datasource and show it again - won't it work as expected?

So something like:
Code: Select all
// hide that form and any 'descendant' forms that inherit from it - if any
// destroy/remove from history 'descendant' forms - if any
if (history.remove('myForm')) {
    // change datasource here
    // show form again (it should reinitialize)
}
Andrei Costescu
Servoy
Andrei Costescu
 
Posts: 1018
Joined: Tue Jun 26, 2007 3:14 pm

Re: Createdatasource as shared datasource on forms

Postby developers10 » Mon Mar 06, 2017 10:04 am

Hi andrei,

I tried that too but the problem is that when you 'destroy' the form by using the History.remove(), everything of that form gets lost or reset.
So variables which held a specific value set by me in runtime, suddenly reverted to their initial value (which could be seen as normal because the entire form gets created again).
The reason is use this not visible form, is for the onRelatedFoundset and other methods which I then use to set or create other forms.
developers10
 
Posts: 31
Joined: Thu Feb 23, 2017 5:33 pm


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 10 guests