Solved

C# Design Time and User Components

Posted on 2010-08-31
6
404 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now