Drop down list server control creates error message

Drop down list server control creates error message

I created a server control that inherits the drop down list control. Basically I wrap up some data access information inside the control. Now all I have to do is drop this control down on a page, set a storedproc and the dropdownlist gets filled. This saves me from having to write all the connection stuff with each drop down I use.

The problem is that when I add this control in design view I get an "Object not set to an instance of an object" dialog box. In the declaration section, I have a reference to a data access business object I wrote. This will take care of running the stored procedure and adding parameters. I also have four methods that mimic the AddParamter methods of the SQLCommand object.

If I override the OnLoad event and create the instance of my DataAccess object there, instead of in the declarations section, everything works fine.

Any ideas why it works when I put it in the OnLoad but not in the declarations? Also, if I put it in the OnLoad, then users can't use this in the Page_Load. They have to use it in the Page_Prerender.

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

Without having the code to look at, it would be nearly impossible to determine the exact cause of the exception.

I would recommend that you review the Control execution lifecycle. If you have the MSDN Library installed, you can find this using the index and the query 'Control Execution Lifecycle', or by simply searching Google for the same phrase.


Do you have comments on this Ask the Expert question and response? Let us know.

This was first published in August 2005