New to Telerik Reporting? Download free 30-day trial

Edit the Report Data Source Components at Runtime

Environment

Product Progress® Telerik® Reporting
Version 15.2.21.1110 and higher

Description

In the Reporting R3 2021 SP1 release of Telerik Reporting, we introduced to the Report Class the method GetDataSources.

The GetDataSources method allows you to access and/or modify any data source component in the report definition, regardless of whether they are referenced by a data item or a report parameter.

An example in which this method will be useful is when the connection string, used by the report data sources, has to change dynamically at runtime.

Solution

Let's assume that the report file is with the TRDP extension, then the following code could be used to unpackage the report and then to edit its connection and even the SQL query:

var query = "SQL_QUERY_HERE";
var reportPackager = new ReportPackager();
Telerik.Reporting.Report report = null;

using (var sourceStream = System.IO.File.OpenRead("Report1.trdp"))
{
    report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);
}

var sqlDS = report.GetDataSources().OfType<SqlDataSource>();

foreach (var sqlDataSource in sqlDS)
{
    sqlDataSource.ConnectionString = "CONNECTION_STRING_HERE";
    sqlDataSource.SelectCommand = query;
}

var irs = new InstanceReportSource() { ReportDocument = report };

Notes

If the above code is to be used for modifying reports displayed by an HTML5-based Report Viewer, the code must be placed in the Resolve method of a custom IReportSourceResolver used by the Reporting REST service.

See Also

In this article