This is a migrated thread and some comments may be shown as answers.
Programmatically change DataSource at runtime
1 Answer 443 Views
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 02 Dec 2014, 12:04 AM
I'm trying to programmatically open a existing Telerik Report, change the connection string, pass-in the paramaters, and export it to PDF.  I am able to open the report, pass parameters, and export successfully but I'm having an issue with changing the connection at runtime.  I've seen several examples in the forum that show how to cast the DataSource of the Report object to it's appropriate type (in this case SqlDataSource), and then set the ConnectionString of that DataSource to what you want it to be.  This does not work for me, as the DataSource is set to Nothing (I'm using VB.NET) when I open the file (or instantiate the Visual Studio class).

I've tried this with both a TRDX file and with a Visual Studio file/class, both have the same problem.  Any ideas why this isn't working for me?

1 Answer, 1 is accepted

Sort by
0
Stef
Telerik team
answered on 04 Dec 2014, 12:31 PM
Hi Charles,

If the designer report uses data source components, you can access the report's and nested data items' DataSource properties, and modify them. For example, check the approach illustrated in the Changing the connection string dynamically according to runtime data KB article.

If a DataSource property is not set at all, the data can be retrieved and assigned at run-time.

Consider the following example:
'MyReport is a type inheriting Telerik.Reporting.Report
Dim report As New MyReport()
 
'if the report is designed to use a SqlDataSource as DataSource
DirectCast(report.DataSource,Telerik.Reporting.SqlDataSource).ConnectionString ="Data Source=(local)\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=SSPI"
 
'if the report has a nested data item, which DataSource is not set at all
Dim table = DirectCast(report.Items.Find("table1",True)(0), Telerik.Reporting.Table)
table.DataSource = GetDataNow()


Once the report is modified and ready, you can wrap it in an InstanceReportSource and pass it to ReportProcessor instance to export the report.


Let us know if you need further help.

Regards,
Stef
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Asked by
Charles
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or