Detecting Design Mode In Visual Studio

If you are using a control or component in Visual Studio in design mode, and if in that component, there is code which attempts to access a database, you will encounter an error when trying to view it, and you will be unable to access the design mode view.

The solution is to check for design mode. For example:

if(this.Site.DesignMode){
     // do stuff here, but don't do a db connection
}
else{
     // do your db stuff here
}

BUT the problem here is that DesignMode shouldn't be used in a constructor. It won't work. A great solution therefore is:

if(System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime){
     // now you've detected DesignMode and it works if the check is even in a Constructor!
}
else{
     // you can do your db stuff here
}


Todd Hopkinson