How to create datasouce programmatically

Find out how to get things done with Servoy. Post how YOU get things done with Servoy

How to create datasouce programmatically

Postby hardina09 » Thu Dec 06, 2012 3:22 pm

I have a form with text field elements to display data in table view and search field and button.
Data provider is not assigned at design time.
I want to create datasource and assign dataprovider to text field programmatically and diplay data in table view

How to display data when any search is entered and hit button.Do I need to again create datasource for my existing form

Please suggest.
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm

Re: How to create datasouce programmatically

Postby jasantana » Thu Dec 06, 2012 6:41 pm

Hi Hardina09.

You can create your data soource from a dataSet with: _dataSet.createDataSource('myDataSource'). Then using solutionModel you can attach that dataSource to your form and set the dataProviders to the elements as well.

Code: Select all
var _dataSet=databaseManager.getDataSetByQuery('MyServer','SQL_Sentence',MyArgsArray,-1);
var _dataSource=_dataSet.createDataSource('MyDataSource');

var _myForm=solutionModel.newForm('MyNewFormName', _dataSource, 'MyStyleName', false, 500,500);
_myForm.extendsForm='ExistingFormName';

// Following must be done with all the fields that you need
var _Field1=_myForms.getField('Field1');
_Field1.dataProvider='NameOfDataProvider';

forms['MyNewFormName'].controller.show();


I think that your ExistingFormName have the button and text fields to do the search. Searching code should be something like:

Code: Select all
function cmdSearch_onAction(event){
   if(foundset.find()){
      foundset['Field_I_Want_To_Search_On'] = FormVarWhereTheUserEntersSearchString;
      foundset.search();
   }
}

It is not a big example but I guess will help you. Just dig in
Best regards,
Juan Antonio Santana Medina
jasantana@nephos-solutions.co.uk
Servoy MVP 2015
Servoy 6.x - Servoy 7.x - Servoy 8.x - MySQL - PostgreSQL - Visual Foxpro 9
User avatar
jasantana
 
Posts: 555
Joined: Tue Aug 10, 2010 11:40 am
Location: Leeds - West Yorkshire - United Kingdom

Re: How to create datasouce programmatically

Postby hardina09 » Thu Dec 06, 2012 8:52 pm

Thanks Juan,

Thank you for reply.
I created form and added above code but different way I am not extending form rather using Main form with sub form which load data using code which you have given in this forum and Main form has search fields.
When form gets loaded in browser it executes above code in onShow() method of subform. Thats working fine
But for searching code in Main Form when I execute below code it throws error : TypeError: Cannot set property "dataSource" of to "mem:MyDataSource"
Code: Select all
function onSearch()
{
var sql= '<some sql query>'
ds=databaseManager.getDataSetByQuery('servername',sql,null,1000)
var dataSource=ds.createDataSource('MyDataSource')       //TypeError: Cannot set property "dataSource" of  to "mem:MyDataSource"

var myForm=solutionModel.getForm('subformName')
myForm.dataSource=dataSource;                       
forms['subformName'].controller.recreateUI()
}

Any idea why it is throwing error TypeError: Cannot set property "dataSource" of to "mem:MyDataSource"
hardina09
 
Posts: 62
Joined: Tue Apr 24, 2012 9:46 pm


Return to How To

Who is online

Users browsing this forum: No registered users and 4 guests

cron