This is a migrated thread and some comments may be shown as answers.

Set ObjectDataSource value at runtime?

4 Answers 707 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 24 Oct 2019, 07:48 AM

Is it possible to set ObjectDataSource value at runtime?

 

We're trying to generate PDF's based on reports with data supplied at runtime. The structure of the object will always be the same but the values need to vary as this is supplied to the API.

 

For example:

public IActionResult GetReport(ReportData data)
        {
            var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            var reportSource = new Telerik.Reporting.UriReportSource();
            var deviceInfo = new System.Collections.Hashtable();
 
            reportSource.Uri = Path.Combine("Reports", "Report.trdp");
             
            // Need to set the value for the objectdatasource here somehow?
 
            Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);
 
            return new FileContentResult(result.DocumentBytes, "application/pdf")
            {
                FileDownloadName = "QuestionnaireReport.pdf"
            };
        }

4 Answers, 1 is accepted

Sort by
0
Paul
Top achievements
Rank 1
answered on 24 Oct 2019, 07:58 AM
I should probably clarify that I'm trying to set the instance at runtime not the type. The instant is supplied as a parameter to the method.
0
Nasko
Telerik team
answered on 28 Oct 2019, 12:39 PM

Hi Paul,

To modify the report at runtime you'd have to create an object instance from the TRDP report file. This is done using the ReportPackager class to unpack the TRDP:

Telerik.Reporting.Report report = null;
using (var sourceStream = System.IO.File.OpenRead("Reports/Report.trdp"))
{
    var reportPackager = new ReportPackager();
    report = (Report)reportPackager.UnpackageDocument(sourceStream);
}

report.DataSource = //set the new ObjectDataSource here

var reportSource = new InstanceReportSource();
reportSource.ReportDocument = report;

var result = reportProcessor.RenderReport("PDF", reportSource, deviceInfo);

To create the new ObjectDataSource use the constructor and then configure the data source to use your data according to the ObjectDataSource supported object types and requirements.

Regards,
Nasko
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Paul
Top achievements
Rank 1
answered on 28 Oct 2019, 03:30 PM
Thanks. I'd managed to work most of that out. However, it get's problematic when you have more than one datasource as you then have to iterate over the items to set the datasource for each chart etc.
0
Nasko
Telerik team
answered on 29 Oct 2019, 09:25 AM

Hello Paul,

If you have a limited number of data items in the report, you can find them inside the report object instance (report.Items.Find) and set their data sources as well. If the items are too many, you can use an approach similar to the one described in Changing the connection string dynamically according to runtime data to set the new data sources.

Regards,
Nasko
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Paul
Top achievements
Rank 1
Answers by
Paul
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or