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

Change Connection String in Subreport

5 Answers 214 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 01 Sep 2011, 06:42 PM
I had created a Master-Detail report.  In this report, the subreport contains a report which displays the detail data.  I want to change the connection string of the subreport because sometimes the location of the Access database changes.  How can I use code to change the connection string (i.e. database location) for the subreport contained in the Master report?

Thanks.

5 Answers, 1 is accepted

Sort by
0
Chavdar
Telerik team
answered on 06 Sep 2011, 05:09 PM
Hello James,

As Telerik Reporting report definitions are standard .net classes you can easily expose a property on the detail report class for the connection string and set it from outside, for example in an event. Let us know if you experience any problems in achieving this functionality.

Regards,
Chavdar
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
James
Top achievements
Rank 1
answered on 10 Sep 2011, 08:00 PM
Hello,

Thanks for the reply.

Could you please provide sample code to show this functionality.


Thanks.

0
Steve
Telerik team
answered on 13 Sep 2011, 12:41 PM
Hello James,

Here is a sample code snippet:
Copy Code
public string ConnectionString
{
    set
    {
        this.sqlDataSource1.ConnectionString = value;
    }
}

Copy Code
var report = new ReportDynamicConnectionString();
report.ConnectionString = @"Data Source=.\SQLEXPRESS2008;Initial Catalog=AdventureWorks;Integrated Security=True";
this.ReportViewer1.Report = report;

Hope this helps.

Greetings,
Steve
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Richard
Top achievements
Rank 1
answered on 09 May 2013, 12:25 PM
What about the sub report?
0
Chavdar
Telerik team
answered on 13 May 2013, 04:20 PM
Hello,

In general the best approach is to use named connection strings which are stored in the application's configuration file. Thus by simply modifying the configuration file you can change the connection strings for all of the reports. More information you can find in the Storing and Retrieving Connection Strings MSDN article. For the reports you just have to specify the name of the connection string for the ConnectionString property of the respective data source component.

If you want to change the connection string programmatically there are different approaches depending on how the reports are represented. For example, if they are xml files (.trdx) you can use the XmlDocument class and replace the attribute value of all elements that contain the ConnectionString attribute. On the other side if your report is a .net class you can use a helper method which returns all subreport's datasources. For example:

static IEnumerable<T> GetDataSources<T>(Telerik.Reporting.Report report) where T : Telerik.Reporting.DataSource
{
    // REPORT
    if (report.DataSource is T)
    {
        yield return (T)report.DataSource;
    }
  
    // SUBREPORTS
    var subReports = report.Items.Find(typeof(Telerik.Reporting.SubReport), true);
  
    foreach (Telerik.Reporting.SubReport subReport in subReports)
    {
        var irs = subReport.ReportSource as Telerik.Reporting.InstanceReportSource;
  
        if (null != irs)
        {
            foreach (var dataSource in GetDataSources<T>((Telerik.Reporting.Report)irs.ReportDocument))
            {
                yield return dataSource;
            }
        }
    }
}

Hope this helps.

Greetings,
Chavdar
the Telerik team

Have you tried the new visualization options in Telerik Reporting Q1 2013? You can get them from your account.

Tags
General Discussions
Asked by
James
Top achievements
Rank 1
Answers by
Chavdar
Telerik team
James
Top achievements
Rank 1
Steve
Telerik team
Richard
Top achievements
Rank 1
Share this question
or