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