Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Design Time and User Components

Posted on 2010-08-31
6
Medium Priority
?
426 Views
Last Modified: 2013-12-17
I have a user control which I have added to a form and am having problems with it when trying to view the parent form in the designer.  The user control displays data from a database and in its constructor pipulates itself using:


// Populate the control
if (!this.DesignMode)
   Populate();

This is all fine when I view the user control itself but on opening the parent form it tries to call the populate function and fails as the database does not exist yet.  I have put break points in  the code but I guess these do not get processed by the designer as its not in debug as such.

How can I prevent run-time logic from being executed by the designer in this way.  I think I must be most of the way there as I can view the user control itself.
0
Comment
Question by:ChrisMDrew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 2000 total points
ID: 33568025
0
 
LVL 12

Expert Comment

by:w00te
ID: 33568555
The user control shoudln't populate itself really.  Allow the containing form to call populate() on the control after safely checking the data source connection (the database in your case).  If you find you don't have a connection, just don't call it (usually someone would log an error in this case if you have a logging mechanism).  Alternately you can also provide a timer and recheck every so often and populate it when you determine you have a connection.
In any case, the form/entity which has a reference to the database connection should be the one in control of whether or not populate gets called.
PS: you may want to rethink your design.  Populate shouldnt really directly reference a database connection that may not exist, all queries of this nature are best going through some sort of central control class for the database.  The class can have built in logic to ensure you have a connection, and to handle the cases were you don't have one.  This would prevent the user control from ever crashing (and anythign else that uses the database).
-w00te
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33568930
I would not recommend to call the Populate method within your constructor. Declare a method say Initialize within your UserControl and invoke that from the Parent Control and that too only at RunTime.

If you still insist in having that in the Constructor, see whether DesignerAttribute will be of help to you.

http://msdn.microsoft.com/en-us/library/system.componentmodel.designerattribute.aspx
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 10

Expert Comment

by:Mathiyazhagan
ID: 33573678
hi, try this out in your user control :

// Populate the control
//if (!this.DesignMode)
 if (System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Designtime)
{
        Populate();
}
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33573737
I'll go with Mathiyazhagan. I remember to have done a similar thing in my code as well. Sorry should have pointed that earlier in my response ;)
0
 

Author Closing Comment

by:ChrisMDrew
ID: 33742047
Apologies for the delay in accepting this - I eneded up on another project and forgot all about it
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question