Programmatically change DataSource at runtime

2 posts, 0 answers
  1. Charles
    Charles avatar
    3 posts
    Member since:
    Apr 2011

    Posted 01 Dec 2014 Link to this post

    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?
  2. Stef
    Stef avatar
    3610 posts

    Posted 04 Dec 2014 Link to this post

    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.


    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.

Back to Top