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"];
}
}
Use
if (HttpContext.Current == null ||
HttpContext.Current.Session == null ||
HttpContext.Current.Session["ShoppingCart"] == null)
instead of
if (Session["ShoppingCart"] == null)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With