Passing Connection String to Sub Report

14 posts, 0 answers
  1. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 04 Feb 2009 Link to this post

    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?
  2. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 05 Feb 2009 Link to this post

    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?
  3. DevCraft banner
  4. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 05 Feb 2009 Link to this post

    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.


  5. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 06 Feb 2009 Link to this post

    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.
  6. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 06 Feb 2009 Link to this post

    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




  7. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 06 Feb 2009 Link to this post

    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.
  8. Andrew Chandler
    Andrew Chandler avatar
    5 posts
    Member since:
    Mar 2009

    Posted 27 Apr 2009 Link to this post

    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. 
  9. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 29 Apr 2009 Link to this post

    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.
  10. Michael
    Michael avatar
    19 posts
    Member since:
    Oct 2008

    Posted 29 Apr 2009 Link to this post

    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.



  11. Andrew Chandler
    Andrew Chandler avatar
    5 posts
    Member since:
    Mar 2009

    Posted 29 Apr 2009 Link to this post

    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.  
  12. Steve
    Admin
    Steve avatar
    10941 posts

    Posted 30 Apr 2009 Link to this post

    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.
  13. Ashok Sridhar
    Ashok Sridhar avatar
    9 posts
    Member since:
    Dec 2006

    Posted 20 May 2009 Link to this post

    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.

  14. Jason
    Jason avatar
    6 posts
    Member since:
    Jun 2009

    Posted 09 Jun 2009 Link to this post

    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 ?
  15. Svetoslav
    Admin
    Svetoslav avatar
    649 posts

    Posted 25 Aug 2009 Link to this post

    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.
Back to Top
DevCraft banner