Solved

C# Design Time and User Components

Posted on 2010-08-31
6
412 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
6 Comments
 
LVL 10

Accepted Solution

by:
Mathiyazhagan earned 500 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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