5

I have a web part in which I have Grid View which displays data from a custom list. In the Page_Load I am initializing the DataTable. I want to store the DataTable object in Session object so that it wont fetch it again and again, similar to what has been done in this example.

Session["data"] = dataTableObject;

But the code shows compile time error.

The name 'Session' does not exist in current context.

I even tried to to use HttpContext from this answer.

HttpContext.Current.Session["data"] = dataTableObject;

But it shows the same error.

3

in your webpart you need to change the web.config:

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

to

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

and also add this in:

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

the above goes within:

<modules runAllManagedModulesForAllRequests="true">

so you need to find the modules tag and add it in, it will look somthing like this:

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

then within your webpart you can do:

to save string:

Page.Session["storethis"] = "hello world";

to get string:

string helloWorld = Page.Session["storethis"].ToString();

also how is your class made? are you inheriting from the webpart class? im asking becasue this error is shown when you dont inheirt from a class (webpart or page) or not set within web.config

as an example this is my redirect class webpart that redirects when on a page that is loaded, it gets the session value that im soring (url) and redirecting:

public class httpRedirectHome : WebPart
{
    protected override void CreateChildControls()
    {
    }

    protected override void OnInit(EventArgs e)
    {

        changeRedirection(Page.Session["PreviousPageUrl"].ToString());


        base.OnInit(e);
    }

    private void changeRedirection(string url)//,string extentionType)
    {
        if (Page.Session["PreviousPageUrl"] != null)
        {
            Page.Response.Redirect(url);
        }
    }
}

as you can see its a webpart that iv made (public) and im inheriting from webpart class

public class httpRedirectHome : WebPart

if it was an aspx.cs it would be somthing like:

public partial class showBifDetails : System.Web.UI.Page

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.session(v=vs.110).aspx

5

You'd need to enable Session State in your web application.

  1. Execute the following powershell script Enable-SPSessionStateService –DefaultProvision

  2. Apply the following web.config change:

    pages enableSessionState="true"
    

More information is available at: Using Session State in SharePoint 2010

  • notice that #1 is actually SharePoint 2010 Management Shell window. normal PowerShell window will throw Enable-SPSessionStateService –DefaultProvision – itsho Feb 16 '15 at 15:08
1

For me I just needed to run this PowerShell Command and it works

Enable-SPSessionStateService -DefaultProvision

After this, the following works

HttpContext.Current.Session["toto"] = "toto";

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.