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

Detect preview mode in code behind

3 Answers 154 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 10 Mar 2011, 05:35 AM
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.

3 Answers, 1 is accepted

Sort by
0
David
Top achievements
Rank 1
answered on 10 Mar 2011, 08:47 PM
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.
0
Peter
Telerik team
answered on 15 Mar 2011, 07:51 PM
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";
#endif

       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,
Peter
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!
0
Zawer
Top achievements
Rank 1
answered on 25 Mar 2011, 05:21 PM
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

InitializeComponent();

 

 

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

 

 

{

 

 

 

    this.Report.DataSource = null;

 

     this.sqlDataSource1.Dispose();

     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.
Tags
General Discussions
Asked by
Michael
Top achievements
Rank 1
Answers by
David
Top achievements
Rank 1
Peter
Telerik team
Zawer
Top achievements
Rank 1
Share this question
or