Detect preview mode in code behind

4 posts, 0 answers
  1. Michael
    Michael avatar
    2 posts
    Member since:
    Feb 2011

    Posted 09 Mar 2011 Link to this post

    Is there any way to detect that you are in preview mode in code behind file?

    What I really want to do is set the DataSource = null in the report class constructor in run-time (so that the NeedDataSource event runs).  But in preview I would like to use the default data source.  Right now I have to comment / uncomment code to achieve this.

    I thought I could put an "if in design mode" similar to the following, but wondered if there is a way to detect this in the code?

    if (GetIsInDesignerPreviewMode(this))
                this.DataSource = null;

    Thanks for your feedback and recommendations.
  2. David
    David avatar
    1 posts
    Member since:
    Apr 2009

    Posted 10 Mar 2011 Link to this post

    I would really like an answer to this as well.  I'm setting a dynamic datasource which works fine when the project is run, but breaks the preview mode.  I would also appreciate the ability to detect preview mode.
  3. DevCraft R3 2016 release webinar banner
  4. Peter
    Peter avatar
    1611 posts

    Posted 15 Mar 2011 Link to this post

    Hello David,

    The Telerik Reporting engine is an embedded reporting engine and as such it is usually hosted inside a 3rd party application (ex. console, Windows Forms, Web). When previewing a report in the Report Designer, the most important thing to notice is that the reporting engine is hosted inside the VS application (devenv.exe) so the devenv.exe.config is the only config file that takes effect. Although any projects inside VS can have config files they are never applied at design time and it is not possible to change this behavior. For more information please see Application Configuration Files in MSDN.

    What happens in your case is that VS fails to load the proper connectionString because it is looking at the wrong place to overcome this behavior we have a few suggestions:

    1. Preview the reports in another application (be it a Windows Forms or Web) outside Visual Studio to ensure that all dependencies are resolved appropriately.
    2. Set the connection string in the master report's detail_ItemDataBinding as shown in the following code snippet:
    #if DEBUG
                var dataSource = (SqlDataSource)detailReport.DataSource;
                dataSource.ProviderName = "System.Data.SqlClient";
                dataSource.ConnectionString = @"Data Source=(local)\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";

           3.Apply any connectionStrings, settings and references to your DAL in the devenv.exe.config file along with copying any external assemblies in the same folder, where devenv.exe resides.

    You can find more info on this in the following KB article as well: Using connectionStrings from configuration file.

    Kind regards,
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  5. Zawer
    Zawer avatar
    2 posts
    Member since:
    May 2007

    Posted 25 Mar 2011 Link to this post

    Hi David,

    I solve this problem by creating a RunTime parameter as boolean and set its default value to False.
    I always set this parameter to true before calling the report in real time.

    Then in Code behind of the report, you can use




    if ((bool)this.ReportParameters["RunTime"].Value)  







        this.Report.DataSource = null;



         this.NeedDataSource += new EventHandler(myReport_NeedDataSource);



    Sorry, even this does not work, cannot read run-time parameter except on the myReport_NeedDataSource, it was just an idea.
Back to Top