How to add datasource using code?

Stimulsoft Reports.JS discussion
Post Reply
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

How to add datasource using code?

Post 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.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: How to add datasource using code?

Post 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.
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

Re: How to add datasource using code?

Post 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.
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: How to add datasource using code?

Post 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.
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

Re: How to add datasource using code?

Post 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);
}
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

Re: How to add datasource using code?

Post by 303248153 »

Any response? I can't even get demo.html work with your code.
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: How to add datasource using code?

Post by HighAley »

Hello.

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

Thank you.
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

Re: How to add datasource using code?

Post 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?
Attachments
demo.zip
(2.23 KiB) Downloaded 605 times
303248153
Posts: 15
Joined: Mon Nov 14, 2016 4:13 am

Re: How to add datasource using code?

Post 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
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: How to add datasource using code?

Post by HighAley »

Hello.

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

Thank you.
Post Reply