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
Can somebody please post the complete code for VB.net? I am not able to have the workaround working for me...
Thanks in advance.
Mauro, VB.NET code would look like:
If Context IsNot Nothing AndAlso Context.Session IsNot Nothing Then
'Your code goes here
Please let me know if it works for you. Thanks.
Thanks a lot of the instructions. i was getting the same issue and was wondering whts wrong, I even modified my web.config but ddnt help. thanks for the tip.
Have a nice day :)