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

Change Report ConnectionString on-the-fly

3 Answers 419 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
BRIAN
Top achievements
Rank 1
BRIAN asked on 20 Nov 2013, 05:54 PM

I cannot get any of the following to work for me:

 

 

I have a WPF project that contains several Telerik Report Q3 2013 objects. The reports also have parameters and they preview just fine. During runtime, each report runs a query that fetches data from a SQL Server database and displays the results in the reports.

 

The WPF app has a menubar. At runtime, a user can go to the Reports menu, and choose to preview Report1, Report2, or Report3. There is a WPF window that hosts a report viewer control (called RptViewer). When the code behind a menu option executes, such as the Report3 menu option, the code basically does the following:

 

    private void mnuReports_View_Report3_Click(object sender, RoutedEventArgs e) {

        InstanceReportSource irSrc = new InstanceReportSource();

        RptViewer winRptView = new RptViewer();

        irSrc.ReportDocument = new Report3();

           

        winRptView.rptViewer.ReportSource = irSrc;

        winRptView.Show();

    }

This all works fine and the correct report is previewed every time. My problem is that each report stores a static connection string.

 

I intend for some of my clients to install and use this WPF app within their own network. The reports will need to access the data within their own SQL Server databases. So I need the connection strings to be updated accordingly. We already have the correct connection strings; we just need the string to be assigned to a report whenever it is loaded by the report viewer. I don’t know why I can’t get this to work.

3 Answers, 1 is accepted

Sort by
0
IvanY
Telerik team
answered on 25 Nov 2013, 01:32 PM
Hello Brian,

In your particular case you have to be using named connection strings so that the reports do not have hardcoded connection strings. Then all you need to do is to change the values of the connection strings upon the installation of your product. This means that you will have to modify the app.config file where the connection strings are stored during the installation - for more information please check this thread.

Regards,
IvanY
Telerik

New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

0
BRIAN
Top achievements
Rank 1
answered on 02 Dec 2013, 07:26 PM

For more than a year, my WPF project has had its own proprietary encrypted XML that stores all database connections. This was a business-logic decision that was made over a year ago, instead of using an app.config. An app.config exists in my WPF project, but we use it for other things. We do not use it to store connection strings. As mentioned already, all connection strings are stored in the XML



When I first added a Telerik report to my WPF project, I noticed that the report wizard created and setup a data source name (DSN) in the app.config. What I am trying to understand is how to programmatically access the report's DSN and assign to it the connection string that is already stored in our encrypted XML. Can you tell me how to programmatically access the report's DSN?



Another way to think of this is like this; imagine that all you simply have is a string variable that currently stores some database connection string. Now, how do I take the value stored in that string variable and assign it directly to the Telerik report without having to go through the app.config?



Thank you

0
Nasko
Telerik team
answered on 05 Dec 2013, 04:50 PM
Hello Brian,

From your application you can access the DSN/SqlDataSource.ConnectionString property programmatically in the following way:
//Create new instance of the report (if the report is .trdx file you will need to deserialize it)
Telerik.Reporting.Report report1 = new Report1();
 
//Get the sql data source assigned to the report
Telerik.Reporting.SqlDataSource sqlDataSource1 = (Telerik.Reporting.SqlDataSource)report1.DataSource;
 
//Get access to the connection string
sqlDataSource1.ConnectionString = "modified string";
 
//Set a report source and display the report in the report viewer
Telerik.Reporting.InstanceReportSource instanceReportSource1 = new Telerik.Reporting.InstanceReportSource();
instanceReportSource1.ReportDocument = report1;
reportViewer1.ReportSource = instanceReportSource1;

Regards,
Nasko
Telerik

New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

Tags
General Discussions
Asked by
BRIAN
Top achievements
Rank 1
Answers by
IvanY
Telerik team
BRIAN
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or