Set ObjectDataSource value at runtime?

5 posts, 0 answers
  1. Paul
    Paul avatar
    4 posts
    Member since:
    Oct 2019

    Posted 24 Oct Link to this post

    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"
                };
            }
  2. Paul
    Paul avatar
    4 posts
    Member since:
    Oct 2019

    Posted 24 Oct in reply to Paul Link to this post

    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.
  3. Nasko
    Admin
    Nasko avatar
    1070 posts

    Posted 28 Oct Link to this post

    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
  4. Paul
    Paul avatar
    4 posts
    Member since:
    Oct 2019

    Posted 28 Oct in reply to Nasko Link to this post

    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.
  5. Nasko
    Admin
    Nasko avatar
    1070 posts

    Posted 29 Oct Link to this post

    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
Back to Top