12

I have the following code, that uses session but i have an error in the line :

if (Session["ShoppingCart"] == null)

the error is cS0103: The name 'Session' does not exist in the current context what is the problem ?

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using System.Collections.Generic;
using System.Web.SessionState;
/// <summary>
/// Summary description for ShoppingCart
/// </summary>
public class ShoppingCart
{
    List<CartItem> list;
    public ShoppingCart()
    {
        if (Session["ShoppingCart"] == null)
            list = new List<CartItem>();
        else
            list = (List<CartItem>)Session["ShoppingCart"];
    }
}
p.campbell
  • 95,348
  • 63
  • 249
  • 319
Adham
  • 61,516
  • 96
  • 221
  • 343

5 Answers5

44

Use

if (HttpContext.Current == null || 
    HttpContext.Current.Session == null || 
    HttpContext.Current.Session["ShoppingCart"] == null)

instead of

if (Session["ShoppingCart"] == null)
Peter
  • 35,292
  • 38
  • 140
  • 191
  • 2
    But I get this exception System.NullReferenceException .. !! – Adham Aug 06 '11 at 23:25
  • Have you abanomed the session or some thing? my guess is that HttpContext.Current is null or HttpContext.Current.Session is null... but i have no idea why with so little info – Peter Aug 07 '11 at 16:47
  • this HttpContext.Current.Session["ShoppingCart"] was cool. thanks :) – Umit Kaya Aug 21 '15 at 04:57
17

The issue is that your class does not inherit from Page. you need to Change

public class ShoppingCart

to

public class ShoppingCart : Page

and it will work

ArunPratap
  • 4,430
  • 7
  • 26
  • 42
Jeff
  • 1,911
  • 4
  • 24
  • 38
8

You either need to convert your class to a Page by inheriting from Page, or have the Session passed in, or use HttpContext.Current.Session.

0

In my case only try-catch block fix problem, like this:

protected void Application_AcquireRequestState(object sender, EventArgs e)
    {
        /// Using from Try-Catch to handle "Session state is not available in this context." error.
        try
        {
            //must incorporate error handling because this applies to a much wider range of pages 
            //let the system do the fallback to invariant 
            if (Session["_culture"] != null)
            {
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(Session["_culture"].ToString());
                //it's safer to make sure you are feeding it a specific culture and avoid exceptions 
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Session["_culture"].ToString());
            }
        }
        catch (Exception ex)
        {}
    }
halfer
  • 19,471
  • 17
  • 87
  • 173
Sayed Abolfazl Fatemi
  • 3,384
  • 2
  • 32
  • 47
0

If you want to use session directly then just simply add following namespace

using system.web.mvc

bhavsar japan
  • 105
  • 1
  • 3