I'm trying to code a visual Webpart in sharepoint2010 where users can fill out a form to order meals, you can order meals for several persons at once. The form fills a static datatable which in turn populate a gridview. The gridview is displayed so you can check your order before you send it.

Now the problem is that orders made by one user is also displayed when another user goes to the site. The datatable is not unique for the session.

Should I avoid to have a static datatable or is there some way to make the session unique?

  • Why do you even need it to be static? Apr 18 '13 at 8:22
  • Right now the datatable is both used to fill the gridview and is later exported to a SPList when the order is done. If it's not static the Datatable will reset each Page_Load right?
    – Skaft
    Apr 18 '13 at 8:24
  • 1
    Don't use static variables when it is not meant to be static or you will get in big trouble. There are so many other ways how to store it (if you really need to). Use session, cookies, cache, etc...
    – luccio
    Apr 18 '13 at 8:27

Make sure your saving the data within session state and not as static, static means regardless of change on load it will revert back to the origonal value!

If your looking for that than you should be setting thoes values within the webpart properties section ;)

for session state look at my answer!

How to save variables per user session per page

for constant strings as default values (set within the webpart properties)

look at my answer here for setting properties:

sharepoint webpart property disappear after a day (C#)

session state will save your data over postback, and the webpart properties will give you default values when you first populate the webpart onto the page! each webpart can than be edited to your default value without effecting any other :)


you need to enable the session state if you get the error:

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive

goto your web application web.config file! if you dont know where it is than look in IIS manager and goto the site and goto the web application files and folders, it should be in the root folder for that web application (virtual folder).

once in you need to add the following:

<pages enableSessionState="true" enableViewState="true" enableViewStateMac="true" validateRequest="false" pageParserFilterType="Microsoft.SharePoint.ApplicationRuntime.SPPageParserFilter, Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" asyncTimeout="7">  

as you can see above enableSessionState="true" and in your case its set to false! make sure that is configured correctly!

lastly add the following if you dont have it:

<modules runAllManagedModulesForAllRequests="true">
   <remove name="Session" />
   <add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="" />

save the web.config and exit, goto the webpage and reload! bingo all works

  • Looks awesome! Will try to implement it right now. Thanks!
    – Skaft
    Apr 18 '13 at 9:05
  • I implemented your code: When trying to go to webpart-page I get this error: 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.
    – Skaft
    Apr 18 '13 at 10:34
  • I have added a web.config-file with the " <add name="Session" type="System.Web.SessionState.SessionStateModule" /> " reference.
    – Skaft
    Apr 18 '13 at 10:37
  • just ammended my answer
    – Ali Jafer
    Apr 18 '13 at 10:46
  • Sir, you are a scholar and a gentleman. Thank you very much.
    – Skaft
    Apr 18 '13 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.