2011-08-06 11 views
7

में मौजूद नहीं है मैं है निम्नलिखित कोड, उस सत्र का उपयोग करता है, लेकिन मैं लाइन में कोई त्रुटि है:'सत्र' वर्तमान संदर्भ

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

त्रुटि cS0103: The name 'Session' does not exist in the current context है क्या समस्या है?

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"]; 
    } 
} 
+0

जो विधि निम्नलिखित जोड़ने सीधे सत्र उपयोग करना चाहते हैं? क्या आपका मतलब एक कन्स्ट्रक्टर है? ऑनर क्लास – Adham

+1

में आप इसे एक गैर सत्र पृष्ठ से कॉल करते हैं - या थ्रेड से। – Aristos

उत्तर

15

मुद्दा यह है कि आपकी कक्षा पृष्ठ से प्राप्त नहीं होती है।
बदलें सार्वजनिक वर्ग ShoppingCart

सार्वजनिक वर्ग ShoppingCart रहे हैं: पृष्ठ

और यह आप

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

पृष्ठ को विरासत पृष्ठ जब यह कोई पृष्ठ उचित समाधान की तरह प्रतीत नहीं होता है ... – Peter

34

उपयोग काम या तो की आवश्यकता होगीPage से विरासत में अपनी कक्षा को Page पर परिवर्तित करें, या Session पास किया गया है, या HttpContext.Current.Session का उपयोग करें।

+2

लेकिन मुझे यह अपवाद सिस्टम.NullReferenceException .. !! – Adham

+0

क्या आपने सत्र या कुछ चीज़ का उल्लंघन किया है? मेरा अनुमान है कि HttpContext.Current शून्य है या HttpContext.Current.Session शून्य है ... लेकिन मुझे नहीं पता कि इतनी छोटी जानकारी – Peter

+0

के साथ क्यों यह HttpContext.Current.Session ["ShoppingCart"] अच्छा था। धन्यवाद :) – Umitk

7

की

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

बजाय

+2

लेकिन मुझे यह अपवाद मिलता है System.NullReferenceException .. !! – Adham

+0

पृष्ठ को विरासत पृष्ठ जब यह कोई पृष्ठ उचित समाधान की तरह प्रतीत नहीं होता है ... – Peter

0

सलाम।

मेरे मामले केवल कोशिश-कैच ब्लॉक ठीक समस्या में, इस तरह:

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) 
     {} 
    } 
0

एक नया पृष्ठ बनाना और उस पर सब कुछ चिपका कर, एक टूटे हुए पृष्ठ से, काम नहीं किया। काम ने विजुअल स्टूडियो को बंद कर दिया और इसे फिर से खोल दिया। टूटी पेज काम किया, बाद में

0

आप तो बस नाम स्थान

using system.web.mvc

संबंधित मुद्दे