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)
3. Workarounds
Thanks. Anh Phan
Wouldn't it be better to check the DesignMode property instead?
Thats a very nice tip.
Thanks,
Thani
Hello,
I created WebApplication Project but i am not able to reproduce it.
Can you please tell me that is there any specific setting create such problem ?
Jinal Patel.
There's no specific setting other than accessing the Session state in the OnInit method. Are you using VS 2010 RTM?
Hi,
Yes i tested it on trail version of Visual Studio 2010 Ultimate edition but i am not able to reproduce it. Control are visible in designer view also and there is no such error.
I am getting this error with the release version of VS 2010. I'm using VB and do access Session in the Page_Init event. What is the VB code I need to include, and should it come before or after the 'InitializeComponent' call?
Thanks
John H
I'm also getting the "Error Creating Control" in Design View on vs2010 (worked fine in 2008 and page runs fine), but not with the session-related error. I'm getting "object not set to an instance of an object".
Any ideas?
Hi John,
The equivalent code in VB.NET would look like:
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
InitializeComponent()
MyBase.OnInit(e)
If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then
Dim count As Integer = Session.Count
End If
End Sub
Thanks.
Hi Nick,
Do you have any code in the code-behind file? If yes, can you post the code here. Thanks.
Can you please put your sample project that cause this error over here ?
I tried following way
1. Test with Website Project
2. Test With WebApplication Project with default application
3. Test with Emply WebApplication Project.
Previously i installed RC release on same machine. When i installed trial version i uninstalled RC release.
None of page i am getting this error.
@WebDevTools
I am unfortunately unable to release any piece the code due to legal agreements. It seems that new pages added to the project behave fine, but I cannot find any difference between the existing ones and the new ones. I ended up just reverting the project back to vs2008 for now. Of note, it is a Web Application project (originally written in vs2003, with .net 1.1) that has been updated to vs2008 and .net 2.0. I have not done the "Convert to Web Application" on all pages, but even pages that are converted, with designer file, still exhibited the same issue. Again, the code still runs and displays fine when compiled.
@Jinal
If you were talking to me, I'm guessing a new project would be fine as new pages in the same project are fine. I have no clue what the issue is, but don't have anymore time at the moment to continue fighting with vs2010.
Please Help !!!
Most of my pages inherit class "MembersPage" and inside of this class i use this:
If Session("UserID") = 0 Then
Response.Redirect("login.aspx")
Else
Before i was getting design error about session state. After i've check about "designmode", I get error "Object Reference not set"
Please Help !!! It goes to the point where i can't use VS 2010 !!!
I am getting this same error. Here is the workaround I am using:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not DesignMode Then
'your code here
After adding the "If Not DesignMode" code, close the Designer, save your changes and Rebuild project. Reopen the designer and the errors should be gone.
I was confused somewhat by the initial post. What I have found, which may well be what was meant, is that, in my Page_Init code, I have to include the check on Session actually existing around any code accessing Session, i.e. it is not sufficient to check for a particular session variable being nothing. E.g. for Alex above
If Session("UserID") = 0 THEN
Response.Redirect("Login.aspx")
(may need another 'else' if it is possible Session hasn't been created at runtime, but in my case this will never happen)
Basically, it seems the init code is run 'stand-alone' when opening the .aspx page in the designer, so the code must be capable of running stand-alone with no errors.
I would expect Southwynd's code would work just as well, and be simpler, but I hadn't read his post when I solved my problem.
Jinal,
I've updated the blog with the repro steps. Hope it'll be clear for you.
JohnH,
The issue was at design time, some objects like Session, are not available. So we need to check for its existence before trying to access it.