How to add datasource using code?
How to add datasource using code?
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.
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?
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.
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?
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.
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?
Hello,
In this case, it is not needed use load() method. Without load() will be created a new report.
Please check the following code:
Thank you.
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;
Re: How to add datasource using code?
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".
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?
Any response? I can't even get demo.html work with your code.
Re: How to add datasource using code?
Hello.
Please, try to initialize your variables outside the function or they will not be available outside the function.
Thank you.
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?
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?
Can you check where is the problem?
- Attachments
-
- demo.zip
- (2.23 KiB) Downloaded 605 times
Re: How to add datasource using code?
Just resolved after I saw viewtopic.php?f=27&t=53911.
The solution is very simple.
Just add
after
I'm happy now 
The solution is very simple.
Just add
Code: Select all
report.dictionary.synchronize();
Code: Select all
report.regData("JSON", "JSON", dataSet);

Re: How to add datasource using code?
Hello.
Yes, you should use synchronize() method to add all columns and table from the JSON to the Dictionary.
Thank you.
Yes, you should use synchronize() method to add all columns and table from the JSON to the Dictionary.
Thank you.