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

Passing Connection String to Sub Report

13 Answers 788 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 2
Michael asked on 04 Feb 2009, 03:33 PM
I am passing a connection string from my web application to my report class library using this technique:

In the Report.cs file I created a second constructor that accepts a string variable as the connection string.  I left the default constructor alone so the report would continue to work during designtime with the default connection string.  This passed-in connection string is working perfectly for the main report, but I am having difficulty getting the sub report to use the connection string that I am passing in.

I have tried updating the connection string of the sub report during these two events (from with the Report.cs file):

NeedDataSource and ItemDataBinding -- neither of these two events seem to allow me to override the default connection string associated with the sub-report.  How can this be accomplished?

13 Answers, 1 is accepted

Sort by
0
Michael
Top achievements
Rank 2
answered on 05 Feb 2009, 04:09 PM
Perhaps I would be able to understand this if I understood the sequence of events:

When does the main report initialize the sub-report?  I don't seem to have the ability to control this.  If I did, I could override the constructor of the sub-report and pass the connection string being used in the main report to the sub-report.

It would seem that any public properties in the sub report are not available until AFTER the sub report has been initialized.  It would further seem that the only time to set the connection string is in the constructor of the sub-report (or the main report for that matter).

Am I wrong here?
0
Michael
Top achievements
Rank 2
answered on 05 Feb 2009, 06:37 PM
This is very frustrating. As I could not get my solution above to work, and am approaching a deadline, I went with this solution in the MainReport:

public MainReport() 
        { 
            /// <summary> 
            /// Required for telerik Reporting designer support 
            /// </summary> 
            InitializeComponent(); 
            this.sqlConnection1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ToString(); 
 
        } 

And with a copy of that in the SubReport:

public SubReport() 
        { 
            /// <summary> 
            /// Required for telerik Reporting designer support 
            /// </summary> 
            InitializeComponent(); 
            this.sqlConnection1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ToString(); 
 
        } 

This works when the site is published and live (and in debug mode on the local machine), but this method breaks the designer.  It would be really nice to have a means of dealing with Report connections strings (both for the main and the sub report(s)) that does not require changes to be made to the code prior to publishing the site.  If anyone has a solution that works with Main Reports and Sub Reports please let me know.


0
Steve
Telerik team
answered on 06 Feb 2009, 09:56 AM
Hi Michael,

Take a look at our Visual Studio Examples and specifically the ProductLine Sales catalog which holds two subreports. Open the subreport, edit the dataset and take a look at the TopCustomersDataSetTableAdapter connection properties. As you can see the connectionString is coming from the Settings.settings file.
Also your approach is completely valid and used in a code library here.

Regards,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Michael
Top achievements
Rank 2
answered on 06 Feb 2009, 03:57 PM
I am unsure how the settings file is going to help me.  On my development machine my connection string is set differently than how it will be in the production environment.  This means that I will need two versions of the settings file.

My thinking was as follows: get the connection string from the web.config file and simply not copy the web.config when moving files from the local development environment to the production server.  Thus the only file that would be different between my two environments would be web.config.  All other files are exactly the same.

The solution you suggests seems no different than this, but now I have web.config out of sync as well as settins.settings.

Am I missing something?

Further, using my current solution, I am getting this error in the designer when opening the master report:

"The variable 'mysubReport1' is either undeclared or was never assigned. "

The error is on this line:
this.subReport1.ReportSource = this.mysubReport1;

But, this variable is declared like this:
private MySubReport mysubReport1;

When I run the sub-report on its own and click "Preview"  (with the connection string being imported from web.config) I get this error:

"Exception has been thrownhe target of an invocation.  Object reference not set to an instance of an object."

So, As I stated in my previous post, this method of getting the connection string works when the site is running, but not in design mode:

ConfigurationManager.ConnectionStrings["MyConnectionStringName"]

The code libary example you point me to seems to assume that this method is valid in design and runtime.

Thanks,

Mike




0
Steve
Telerik team
answered on 06 Feb 2009, 04:26 PM
Hi Michael,

The whole idea is to use to keep the connection string on a centralized place, so that you do not have to override it in every single report and keep reference to it directly in the dataset for your reports. Note that this is not something we came up with, both approaches have been thought of and introduced in the .NET framework for such purposes and it is up to you to decide which is easier for you. As for the code library - I haven't tried it with a subreport, so we would further investigate what is going on.

Best wishes,
Steve
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Andrew Chandler
Top achievements
Rank 2
answered on 28 Apr 2009, 01:34 AM
I have the same issue and want to accomplish the same thing.  I want to pull the connection string from the web.config file for the reports I am generating.  When I set the instance object sqlConnection1.ConnectionString = ConfigurationManager....  etc in the constructor, I receive the same error at design time of
"Exception has been thrownhe target of an invocation.  Object reference not set to an instance of an object."

So, how do I set the connection string to use the web.config connection string and have it work in Design time?

I feel Telerik needs to make a very straight forward way of using the connection string from the web.config in the reports module like the SqlDataSource works.  When you select the new connection string there, it shows the ones in the config file. 
0
Steve
Telerik team
answered on 29 Apr 2009, 10:26 AM
Hello Andrew,

Getting the connectionString from your app config file is not something we have thought of and we do not believe that we should implement "custom" solutions as this is the way to go. Telerik Reports are standard .NET classes and the report wizard creates automatically for you typed dataset which is used for binding the report. So abstract yourself from the fact that you're working with 3rd party product and look upon Telerik Reports as standard class getting data from typed dataset. How would you proceed normally in this case?

Kind regards,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael
Top achievements
Rank 2
answered on 29 Apr 2009, 12:18 PM
Hi,

I believe we may be talking past each other on this one.  I have no problem with coming up with my own method of assigning a connection string.  I am using this method:

this.sqlConnection1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ToString(); 

The problem I have is that this valid method breaks the Reports tool in Designer Mode.  I have to comment out that line before I can work with reports in Designer Mode -- or at the very least before I can preview them.

So the problem is not that Telerik needs to invent some formal method of passing connection strings, but rather ensure that the designer does not break when valid methods are used.



0
Andrew Chandler
Top achievements
Rank 2
answered on 29 Apr 2009, 05:33 PM

Q3 2008 SP2 (version 2.9.9.130)

2/2/2009 12:00:00 AM

 

Fixes and Improvements


Visual Studio Designer

  • Fixed exception "Unsupported page unit"
  • Added support for connection strings from the application's config file for Data Source and Report Wizards.
  • Added support for setting styles for multiple report items at once
  • Added option for disabling the Report Wizard occurance

Doesn't this mean that for a web app it would be in the web.config file and for a form app it would be in the app.config file for design time support?  For web apps this does not work as stated above.

I am setting the connection string in the constructor (older workaround), and commenting it out to work just the same to make the design time work.  The Datasource wizard for Parameters allows for selecting the connection string by the config file, just not the report data source wizard.  I would assume because it creates an external datasource, and not objects on the form, that it is using Microsoft's Datasource wizard, this allowing the connection string settings.  We just need the report datasources to do the same for internal report datasource objects (Reference to the ConfigurationManager connection strings) so we are not having to set them ourselves.

Here is an image of the Telerik Parameter Wizard showing .config connection string.  
0
Steve
Telerik team
answered on 30 Apr 2009, 06:40 AM
Hi guys,

@Michael: Got it. We would see what we can do here.
@Andrew: This seems like a bug and we're looking into it now. It would most probably be present in the Q1 SP1 expected within a few days.

Kind regards,
Steve
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ashok Sridhar
Top achievements
Rank 1
answered on 20 May 2009, 07:05 PM
Hi,

Is there any fix for this.  I'm in the same situation.  Is there any event that gets fired when a subreport is being instatiated.  That way I guess I would be able to set the datasource of the subreport to that of the main report.

0
Jason
Top achievements
Rank 1
answered on 09 Jun 2009, 08:23 PM
I am in agreement with michael in terms of the report working in design time. Using Telerik 2009 reporting Demo Q1 currently. When using a valid method that makes a call to the app.config the report errors out in design time;
this.sqlConnection1.ConnectionString = ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ToString(); 

Was there ever a fix for this ?
0
Svetoslav
Telerik team
answered on 25 Aug 2009, 02:31 PM
Hi guys,

Let me elaborate a little more on the problem why using the config files/ConfigurationManager don't work at design time, especially when previewing a report and what is the right way of using connection strings from the config file that work both run time and design time.

The Telerik Report Designer operates inside the Visual Studio application. When previewing a report, the project that contains the report is compiled (if needed) and the report class is instantiated (through Reflection), and passed to the previewer. This means that the report constructor is executed in the context of the Visual Studio application. As described in MSDN,  the ConfigurationManager.ConnectionStrings property "gets the  ConnectionStringsSection data for the current application's default configuration".

In the case of report designer/preview the current application is devenv.exe so reading the  ConfigurationManager.ConnectionStrings from the report being previewed will get all connection strings defined in the devenv.exe.config file.

Obviously this is not the right config file that one may expect, so the trick is to use the application settings as we've previously advised. This is what the Report and Data Source Wizards actually do when instructed to save the selected connection string in the config file.

In this case the connection strings are not only added to the config file but a specific settings are registered in the Settings.settings XML file (and the corresponding code behind). To access the connection string this way one should use the Settings class like this:

C#:
global::ClassLibrary2.Properties.Settings.Default.AdventureWorksConnection 

VisualBasic.NET:
My.Settings.AdventureWorksConnection  

At design time, during preview of report that uses a connection string from the Settings object, as there is no such connection string in devenv.exe.config, a value from the settings metadata will be returned. At runtime the Settings object will return the connection string from the "correct" config file as it is the current application's config file.

There are two things to consider:
  1. Always modify the application settings at design time through its editor and never change it through the config file because the changes won't be recorded in the settings metadata;
  2. If the reports are defined in a class library and you add settings for this class library, always remember to copy the corresponding connection string entries from the class library's config file to the application's config file. Otherwise at runtime when the application's config file is respected the Settings object will find no such connection strings and will return the default value from its metadata.

@Ashok: Please consider handling the SubReport.NeedDataSource event that is raised when the nested report is about to be processed and still has no data source attached. 

Kind regards,
Svetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
General Discussions
Asked by
Michael
Top achievements
Rank 2
Answers by
Michael
Top achievements
Rank 2
Steve
Telerik team
Andrew Chandler
Top achievements
Rank 2
Ashok Sridhar
Top achievements
Rank 1
Jason
Top achievements
Rank 1
Svetoslav
Telerik team
Share this question
or