0
votes

I've been struggling for a week now, I can't find a solution for my problem. I'm fairly new to Sharepoint and want to create a simple webpart.

For this, I need a Session where I can store my DataTable, which contains data from a database.

E.g.:

private void storeDataTable(DataTable dt)
{
    Session["dtSession"] = dt;
}

When I'm debugging this code, an exception is thrown:

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

What I've done so far, to solve this issue:

  1. Tried to enable SessionState in my ascx.cs - markup

    <%@ Control Language="C#" AutoEventWireup="true"  
    CodeBehind="MyWebPart.ascx.cs"      
    Inherits="MyWebPart.VisualWebPart.VisualWebPartUserControl" %>
    <%@ Page EnableSessionState="true" %>
    

    When I am running this, I got an exception:

    server tag is not well formed.
    
  2. Add following to the web.config under "C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config"

    <pages enableSessionState="true" enableViewState="true" enableViewStateMac="true">
    

    and

    <httpModules>
        <add name="Session" type="System.Web.SessionState.SessionStateModule" />
    </httpModules>
    

    3.Run the following in the SharePoint 2010 Management Shell:

    Enable-SPSessionStateService –DefaultProvision
    

    4.Activate the ASP.NET State service under services.msc and change start to automatic.

None of this steps could resolve my issue.

1
No, but as far as I know, I only need the In-Proc-Mode, which is default? I've followed this guide to enable SessionState: nikpatel.net/2012/02/12/…user2508738
InProc mode will only work i you have a single WFE or if you have persistence configured on your load balancer. Limiting yourself to it is a poor design decision if there is any chance your application could need to scale past one server.Brian P
Thanks for your reply, but I have to ask: Is the InProc mode the reason, why the SessionState couldn't be enabled? What if there isn't any chance my application need further server resources?user2508738
no, the inProc mode is the default mode and usualy doesn't create any problem. but, is your webpart integrated inside an other control ?Sebastien H.

1 Answers

0
votes

I was able to solve my issue myself. I've searched through both of the web.configs (VirtualDirectories\80 and VirtualDirectories\7099) and deleted some entrys. Now I got the following entries in my web.config:

<httpModules>
     <remove name="Session" />
     <add name="Session" type="System.Web.SessionState.SessionStateModule" />
</httpModules>

and

 <pages enableSessionState="true" enableViewState="true" enableViewStateMac="true" enableEventValidation="true" etc...>

I guess my fault was to have another SessionState declared which was costum. I've deleted these entries and the application works like a charm. So it seems, the exception will also be thrown, when there are to many Sessions declared.