Solved

C# Design Time and User Components

Posted on 2010-08-31
6
413 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

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
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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