Set datasource for table in TRDP reports programmatically and in runtime

0 Answers 65 Views
.NET Framework Report Designer (standalone) Report Viewer - HTML5 WebForms Serialization
Adrian
Top achievements
Rank 1
Adrian asked on 08 Oct 2021, 02:30 AM | edited on 08 Oct 2021, 10:36 AM

I'm using a HTML5 Web Forms Report Viewer

Here is my code:

            var reportPackager = new ReportPackager();

            var reportInstance = new Report();

            using (var sourceStream = System.IO.File.OpenRead("PathToTRDPFile"))

            {

                reportInstance = (Report)reportPackager.UnpackageDocument(sourceStream);

            }

            var table3 = reportInstance.Items.Find("table3", true)[0] as Telerik.Reporting.Table;

            table3.DataSource = CreateDataTable(); //A function that returns a datatable

            var instanceReportSource = new InstanceReportSource { ReportDocument = reportInstance };

            this.reportViewer1.ReportSource = instanceReportSource;

The error that I got: 

Error CS0029 Cannot implicitly convert type 'Telerik.Reporting.InstanceReportSource' to 'Telerik.ReportViewer.Html5.WebForms.ReportSource'

Any idea on how to resolve this?

Neli
Telerik team
commented on 12 Oct 2021, 02:54 PM

Hi Adrian,

I suspect that you have placed this code in the code behind of the page of the viewer. You need to create a Custom Report Source Resolver and the logic for assigning the datasource in the Resolve method:

class CustomReportSourceResolver : IReportSourceResolver
{
    public Telerik.Reporting.ReportSource Resolve(string reportId, OperationOrigin operationOrigin, IDictionary<string, object> currentParameterValues)
    {
       var reportPackager = new ReportPackager();

            var reportInstance = new Report();

            using (var sourceStream = System.IO.File.OpenRead("PathToTRDPFile"))

            {

                reportInstance = (Report)reportPackager.UnpackageDocument(sourceStream);

            }

            var table3 = reportInstance.Items.Find("table3", true)[0] as Telerik.Reporting.Table;

            table3.DataSource = CreateDataTable(); //A function that returns a datatable

            var instanceReportSource = new InstanceReportSource { ReportDocument = reportInstance };

           return instanceReportSource;
    }
}

Then in the ReportsController, set the ReportSourceResolver = new CustomReportSourceResolver().

Please, test this approach and let me know if everything works on your end.

No answers yet. Maybe you can help?

Tags
.NET Framework Report Designer (standalone) Report Viewer - HTML5 WebForms Serialization
Asked by
Adrian
Top achievements
Rank 1
Share this question
or