Page 1 of 1

How to add datasource using code?

Posted: Mon Nov 14, 2016 4:18 am
by 303248153
Hello, I want create a new report with a custom datasource, but don't known how to do.
Here is my code

```
var report = new Stimulsoft.Report.StiReport();
report.load(null);
var dataSet = new Stimulsoft.System.Data.DataSet("DataSet");
dataSet.readJson({ "DataSet": [ { Id: "a", Name: "A" } ] });
report.regData(dataSet.dataSetName, "", dataSet);
viewer.report = report;
```

After I do this there nothing under "Directory" => "Data Source".
How can I do it right? thanks.

Re: How to add datasource using code?

Posted: Mon Nov 14, 2016 6:38 am
by Alex K.
Hello,

Please check the documentation:
https://www.stimulsoft.com/en/documenta ... report.htm

Also, you can find samples at the following link:
https://github.com/stimulsoft/Samples-JS

Thank you.

Re: How to add datasource using code?

Posted: Mon Nov 14, 2016 6:44 am
by 303248153
Thanks for your reply.
I already read them both very carefully.

First, https://www.stimulsoft.com/en/documenta ... report.htm didn't work for me, I already use DataSet, look my code.
The different is I want to create a new report, so I pass ***null*** to report.load.

Second, https://github.com/stimulsoft/Samples-JS didn't work for me too, it use default templates which is I don't have
Can you provide an empty template(mrt) json?
When I pass {} to report.load, like report.load({}), it just popup a "Error" dialog.

Re: How to add datasource using code?

Posted: Mon Nov 14, 2016 7:06 am
by Alex K.
Hello,

In this case, it is not needed use load() method. Without load() will be created a new report.
Please check the following code:

Code: Select all

var report = new Stimulsoft.Report.StiReport();
var json = { "DataSet": [ { Id: "a", Name: "A" } ] }
var dataSet = new Stimulsoft.System.Data.DataSet("JSON");
dataSet.readJson(json)
report.regData("JSON", "JSON", dataSet);
designer.report = report;
Thank you.

Re: How to add datasource using code?

Posted: Mon Nov 14, 2016 7:24 am
by 303248153
Thank you but that still don't work for me.
You can replace "setReport" in "https://github.com/stimulsoft/Samples-J ... /demo.html" with following code.
After you click "Design" button at top right you should see there nothing under "Data Sources".

Code: Select all

function setReport(reportObject) {
	// Forcibly show process indicator
	viewer.showProcessIndicator();
	
	// Timeout need for immediate display loading report indicator
	setTimeout(function () {
		var report = new Stimulsoft.Report.StiReport();
		report.dictionary.databases.clear();
		var json = { "DataSet": [ { Id: "a", Name: "A" } ] };
		var dataSet = new Stimulsoft.System.Data.DataSet("JSON");
		dataSet.readJson(json);
		report.regData("JSON", "JSON", dataSet);
		viewer.report = report;
	}, 50);
}

Re: How to add datasource using code?

Posted: Tue Nov 15, 2016 1:49 am
by 303248153
Any response? I can't even get demo.html work with your code.

Re: How to add datasource using code?

Posted: Wed Nov 16, 2016 12:05 pm
by HighAley
Hello.

Please, try to initialize your variables outside the function or they will not be available outside the function.

Thank you.

Re: How to add datasource using code?

Posted: Tue Nov 22, 2016 4:04 am
by 303248153
Hello, here is my modified "demo.html", it should display something under "Data Sources" but it didn't.
Can you check where is the problem?

Re: How to add datasource using code?

Posted: Tue Nov 22, 2016 4:13 am
by 303248153
Just resolved after I saw viewtopic.php?f=27&t=53911.
The solution is very simple.
Just add

Code: Select all

report.dictionary.synchronize();
after

Code: Select all

report.regData("JSON", "JSON", dataSet);
I'm happy now :D

Re: How to add datasource using code?

Posted: Tue Nov 22, 2016 11:52 am
by HighAley
Hello.

Yes, you should use synchronize() method to add all columns and table from the JSON to the Dictionary.

Thank you.