• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

C# Design Time and User Components

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
ChrisMDrew
Asked:
ChrisMDrew
1 Solution
 
MathiyazhaganCommented:
0
 
w00teCommented:
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
 
Gururaj BadamCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MathiyazhaganCommented:
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
 
Gururaj BadamCommented:
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
 
ChrisMDrewAuthor Commented:
Apologies for the delay in accepting this - I eneded up on another project and forgot all about it
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now