Background:
I have created this UserControl. In the constructor of the user control I call a function that retrieves some values from the database. If an error occurs while retrieving the values a messagebox explaining the error is shown. So far so good.
The Problem:
I have created a form that (among other elements) includes my UserControl. Now when I open this form (or even the UserControl itself) the constructor is called (I suppose so it can be drawn accurately) and, because the database is unavailable, the messagebox (explained above) is shown.
How do I prevent this from happening?
I just want to be clear: the code works great in runtime. Everything is as designed. It is only in Designer view of Visual Studio (2008 SP1 if it matters) where the problem occurs. However in Designer it is awful, especially now when the application attempts to reconnect when the connection fails. Every time I go in to Designer mode my Visual Studio freezes for about 20 seconds (timeout of the reconnect) and it is killing my work process.
I get around this by having a global static property on my Program class called IsRunning.
When my program starts up in the main method I set the IsRunning property to true. Then in the constructor of my user control I am able to poll the property IsRunning to determine whether or not I execute particular code, in your case code that would attempt to access a database…
EDIT:
Here is some code…
….
In my user control…