Your official information source from the .NET Web Development and Tools group at Microsoft.
In VS 2010, if you are accessing the Session state in the OnInit(…) method of your page, similar to the following code snippet:
then you will encounter an “Error Creating Control” for ASP.NET controls when viewing the Design view of the page. For example, if you have an ASP.NET button control on the page, the designer will display the error:
“Error Creating Control – Button1 Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System. Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules> section in the application configuration.”.
Screen shot of the error:
We are seeing this error because at the design time, some objects such as Session, are not available, or being null. However, the project still runs perfectly at runtime.
To workaround the issue, you can add code to check for the existence of the Session object as shown below, then the design view will render correctly.
We are considering adding a fix for this issue during the VS 2010 SP1 timeframe.
UPDATE: There are some confusion from the readers about the post, so I’m updating the blog to clarify the issue and the workarounds.
1. Steps to reproduce the issue for a website
2. Steps to reproduce the issue for a Web Application Project (WAP)
Thanks. Anh Phan
Now i am getting that error.
Does the workaround mentioned by southwynd or JohnH work for you?
I'm seeing what I think NickH was seeing. Converted a project from ASP.NET 1.1 to Visual Studio 2010. (Tried 4.0, 3.5, 2.0 with the same results.) Certain pages load fine. Other pages get the "Error Creating Control - Object reference not set to an instance of an object." message for all asp controls (html controls show fine).
I thought it might be related to the fact that we were using Infragistics, but I've upgraded the app to the latest versions (tried the 3.5 and 2.0 controls) with no sucess. Plus, some of the pages that work have the same infra controls on the pages that don't, so I'm not sure that's what it is.
I've found that if I change the name of the "Inherits" file (to something that's wrong), and then change it back, suddenly the page works. But if I close and re-open the page it's bad again.
I changed the namespace, and it also worked. This time it would work even if I closed and re-opened the page. But when I closed the project and re-opened...errors again.
As with the other guys, everything works fine at run-time.
Have you guys made any more discoveries on this stuff since the last posts? Thanks.
Do you have code inside OnInit() method? If so, can you post your code here? Thanks.
Are you saying that a new page with the exact same content in the code behind file would work fine, but the page converted from VS 2008 would show error in the design time?
I have no code inside any OnInit() methods that I can find anywhere. I'll see if I can put together some simple code, but I know at one time I had the page stripped down to only an edit box (and did the same with another page that worked). One continued to show the error while the other worked. Which makes me think the real issue is outside the individual page...maybe a project setting or something? But something that is tied to that page name/class, etc?
Somehow when I change the code-behind class name then it re-initializes something that otherwise is not being handled and then it works. Because I don't have to change ANY actual code in the code behind...I just have to tell the .aspx to point to something else...then when I change the code-behind class name back, the page still works. (Until I close and open it again.)
I'm just wondering if you guys understand more of the rendering path behind the scenes, maybe you can figure out what paths it might take differently and that might point you to the issue.
One thing I'm going off to try now...removing the file from the project, then re-adding it (so it's "new"). Maybe that will cause it to work? I'll let you know.
Can you send me the project that reproduces the issue to anhphan AT microsoft.com? That would really help with the investigation. Thanks.
Just to clarify something above. It is NOT changing the CodeBehind name that causes the controls to render, but rather changing the Inherits name. I messed that up in my last post.
I'll see what I can do about sending you some code.
The problem seems to be that VS 2008 didn't run the OnInit method in the actual page at design time, but VS 2010 does.
If you have where events are wired up in the OnInit event (e.g. how VS 2003 used to do it, if you have apps upgraded from .NET 1.1), you get null reference exceptions all over the page. e.g.:
this.registerButton.Click += new EventHandler(this.registerButton_Click);
Even if you mode the event wirup after the base.OnInit, it still messes up the designer. This does seem to be a rather serious oversight, considering the page runs fine and VS 2008 didn't have this bug.
We have an additional blog discussing the OnInit() method being called at the design time, http://blogs.msdn.com/webdevtools/archive/2010/05/06/another-error-creating-control-in-the-design-view-with-object-reference-not-set-in-visual-studio-2010.aspx.
LukeT sent me the sample proeject and it turned out that he did have code inside the OnInit() method. Applying the workaround works fine for him.
I'm having the same problem as Luke and others in that I have a 2.0 framework project that works perfectly fine in VS2008. I upgraded the project into VS10 and while the project builds fine, I cant open any of the web pages in the designer. If I make a change to the "Inherits" line, it then magically renders correctly in the designer, however, If I close the page, and re-open it, again, its wont render.
This is shocking to me that something of this magnitude was able to make it into the final version as this is a HUGE bug on the MSFT side. I cant use VS 10 until this serious issue is corrected.
Update - my bad - if there is ANY code in the OnInit event, you need to use the workaround described by WebDevTools. One note, make sure you re-build the site before attempting to re-open the page and it now renders. A bit annoying that I have to add this code, but Ill take it for now!
This worked for me because after hours of searching i came across this page.
The only question i have is why is there code running when you are looking at the page in the designer? Why are page events being fired at design time? I'm sure many more people will have the same headaches over this as i did.
This work for me:
you have to check Session object and also Context object inside all Init routine:
i.e. : implementation of
a) protected override void OnInit(EventArgs e)
b) void WPPBasePage_Init(object sender, System.EventArgs e) registerd with :
this.init += new EventHandler(YourBasePage_Init);
void YourBasePage_Init(object sender, System.EventArgs e) registerd with :
this.init += new EventHandler(YourBasePage_Init);