This is a migrated thread and some comments may be shown as answers.

Programmatically change DataSource at runtime

1 Answer 1370 Views
General Discussions
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.

 
Ali
Top achievements
Rank 1
commented on 22 Jun 2022, 06:29 AM

Dim report As New MyReport()

can you please share this class code.

Lance | Manager Technical Support
Telerik team
commented on 23 Jun 2022, 02:34 PM

Hi Ali,

'MyReport' is just a standard Telerik Reporting type Report class with the name "MyReport":

Public Partial Class MyReport
    Inherits Report
End Class

However, this is old code from 2014. It is no longer relevant because it creates an instance of the report class (today, you use the type name in a TypeReportSource).

I see you have already opened another forum post with the rest of your code and what you're trying to do. Somebody will follow up with you there with further guidance.

Notes:

  • I noticed you haven't activated your Telerik account yet, this can prevent you from using certain support features. Please click the activation link that was sent to your email (check your email's spam folder if you don't see the email).
  • If you want a faster reply, or a more custom answer that isn't in the scope of public forums, you can open a Support Ticket here.
Tags
General Discussions
Asked by
Charles
Top achievements
Rank 1
Answers by
Stef
Telerik team
Share this question
or