14

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
  • 98,673
  • 67
  • 256
  • 322
Adham
  • 63,550
  • 98
  • 229
  • 344

5 Answers5

47

Use

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

instead of

if (Session["ShoppingCart"] == null)
Peter
  • 37,042
  • 39
  • 142
  • 198
  • 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,816
  • 7
  • 25
  • 43
Jeff
  • 2,061
  • 4
  • 27
  • 45
9

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,824
  • 17
  • 99
  • 186
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
0

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

using system.web.mvc
desertnaut
  • 57,590
  • 26
  • 140
  • 166
bhavsar japan
  • 125
  • 1
  • 4