Adham Adham - 1 year ago 77
ASP.NET (C#) Question

'Session' does not exist in the current context

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>();
list = (List<CartItem>)Session["ShoppingCart"];

Answer Source

The issue is that your class does not inherit from Page.
Change public class ShoppingCart

to public class ShoppingCart : Page

and it will work

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download