Another Error Creating Control in the Design View with Object Reference Not Set in Visual Studio 2010

Another Error Creating Control in the Design View with Object Reference Not Set in Visual Studio 2010

Rate This
  • Comments 10

In a previous blog, Controls State: "Error Creating Control" in the Design View in Visual Studio 2010, I have discussed about an Error Creating Control issue related to accessing the Session state in the OnInit() method. Since then, I have discovered that the Error Creating Control will also show in the design view if we try to access any object not being instantiated at the design time inside the OnInit() method.

For example, in a web application project, in a code-behind file that is adding controls dynamically to a page, similar to what was discussed in the article How To Dynamically Add Controls to a Web Page , we have code that’s accessing an object that is null at the design time as shown below

OnInit

When you switch to the design view, you would see "Error Creating Control – xxx. Object reference not set to an instance of an object”, where xxx is a control on your page. Screen shot of the error is shown below

Error

A simple workaround for this issue would be to check if we are in the design mode at the top of the OnInit() method, below the base.OnInit() call.

FixOnInit

This is a bug in VS 2010 that we’re now calling the OnInit() method at the design time; we are considering a fix for this in a future release of Visual Studio.

Thank you,
-Anh
Visual Web Developer Team

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
  • I get that error too!  I tried the suggested fix and it worked for me!  I did have to restart Visual Web Developer after removing my oninit method.  Now, all my controls are back!  Thanks!!

  • This is not a bug in VS 2010, if you have 2 or more Webforms with the same

    form id, this did occur here. When I did change the form id, it did work

    here.

  • Changing form id worked for me. Thanks!

  • I have one master page with any others forms that ihnerits this master page. I have the same problema in all the forms. this code not work for me.

  • -1 for Microsoft.

    Changing form ID worked for me as well

  • same situation as Giovane. SUggested workaround didnt work with master pages... :-( When there will be fix?

  • @Hermann, Aarti, and Scotty,

    I don't see the issue when having 2 or more WebForms with the same form ID. Can you please elaborate what you do a little bit more? Do you have code inside the OnInit() method at all?

    Thanks.

    -Anh.

  • Giovane and Chorpo,

    Where did you put your code in the MasterPage that causes this issue in all derived content pages? We need to reproduce the issue that you are experiencing.Thanks

    -Anh

  • I had the same issue with Ajax 4.0 and Visual Studio 2010.  Wrapping the check for Me.DesignMode around my Init code fixed the problem.  I also had to restart Visual Studio.

    Thanks!

  • Working fine thanks :)

Page 1 of 1 (10 items)