Having trouble injecting JSON data to report at runtime

1 Answer 366 Views
DataSource JSON Rendering
Jonas
Top achievements
Rank 1
Jonas asked on 26 Oct 2022, 01:58 PM | edited on 26 Oct 2022, 02:00 PM

Hi all,

I have a report using the designer.  I attached a JSON data source in the report, just for structure usage purposes:

{
	"Data": [
		{
			"OrganizationId": "",
			"OrganizationUuid": "",
			"ParentOrgId": "",
			"OrganizationName": " ",
			"TotalUserCount": 0,
			"TotalDeviceCount": 0,
			"LicensedUserCount": 0,
			"ExpirationDate": "",
			"hasChildren": true
		}
	]
}

When I try to inject the actual JSON data at runtime, the report is still generated with the sample data.  Here's the lines generating the JsonDataSource:

            var data = new ReportData { Data = result };
            var ds = new JsonDataSource
            {
                Source = JsonConvert.SerializeObject(data),
                DataSelector = "$.Data",
                Name = "jsonDataSource1"
            };

Then I assign the data source to the report:

                using (var sourceStream = System.IO.File.OpenRead(_path + "\\Reports\\" + myRpt.ReportName))
                {
                    var reportPackager = new ReportPackager();
                    report = (Report)reportPackager.UnpackageDocument(sourceStream);

                    var dtsrc = new ObjectDataSource();
                    dtsrc.DataSource = myRpt.GenerateReport(_parameters);  // this is the JsonDataSource from above
                    dtsrc.Name = "jsonDataSource1";
                    report.DataSource = dtsrc;
                }

When I render the report, the data is not injected, it just shows the single sample record from the designer.  If I inject the actual JSON in the designer and do a preview, the report looks correct, so my guess is that I'm not doing this correctly.  Any help would be greatly appreciated.

Regards

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 31 Oct 2022, 09:42 AM

Hello Jonas,

Thank you for the provided information and the code snippets!

Assigning the Data

I would like to begin by saying that when editing a report through code, already instantiated data objects do not have to be wrapped in a data source component. Instantiated data objects can be set directly to data items' DataSource properties.

What I mean by the above is that the following can be done:

using (var sourceStream = System.IO.File.OpenRead(_path + "\\Reports\\" + myRpt.ReportName))
                {
                    var reportPackager = new ReportPackager();
                    report = (Report)reportPackager.UnpackageDocument(sourceStream);
                    report.DataSource = myRpt.GenerateReport(_parameters);  // this is the JsonDataSource from above
                }

It doesn't even need to be wrapped in JsonDataSource so if you can access the methods you can it like this:

var data = new ReportData { Data = result };
report.DataSource = data.Data;

Using the updated Report instance

After you have updated the data, you need to wrap the Report object in InstanceReportSource before it can be used by the reporting engine:

            var irs = new InstanceReportSource
            {
                ReportDocument = report
            };

From here on, you can use the instance reportsource with a report viewer - Preview a report in the report viewer through an InstanceReportSource - Telerik Reporting,  or with the ReportProcessor - Generating Reports Locally - Telerik Reporting.

Data Items

Lastly, I would like to suggest making sure that the Report.DataSource is the item whose data source needs to be updated.

For example, let's say that you have a table in the detail section that uses its own data source. In that case, updating the report.DataSource will not update the data source used by that table. Instead, the table.DataSource should be edited in the code in order for it to use the new data, for example:

                // Set the data source for the report
                report.DataSource = MyService.GetMyData();
                // Set the data source for another data item
                var table1 = report.Items.Find("table1", true)[0] as Table;
                table1.DataSource = MyService.GetMyTableData();

Wrapping Up

I hope that the provided information will help you resolve the data issues, please do not hesitate to let me know if you have further questions.

Regards,
Dimitar
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Tags
DataSource JSON Rendering
Asked by
Jonas
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Share this question
or