rumi
rumi

Reputation: 3298

how to read value from a session object

Using sql server stored procedure in Linq I m storing a list of questions in a session object as below.

DataClassesDataContext myContext = new DataClassesDataContext();
var QuestionList = myContext.sp_GetAllQuestions().ToList();
Session["QuestionsList"] = QuestionList;

How can I read or cast the value from this session object like

var QuestionList= Session["QuestionsList"]

Sql stored procedure looks like this

SELECT top 24 ROW_NUMBER() OVER (ORDER BY QuestionID) AS QuestionNo,Q.QuestionID,Q.Question,Q.Choices,Q.CorrectAnswer,
 Q.RelatedInfo,Q.QuestionType,CS.Section FROM Questions Q left join dbo.ChapterSection CS on Q.SectionID=CS.SectionID

Upvotes: 2

Views: 3000

Answers (5)

Erik Philips
Erik Philips

Reputation: 54628

I'm not a fan of the week typed session because of the need to type cast each use, so I wrap all my types/objects in a strongly typed session wrapper.

Wrapper Base:

public abstract class SessionBase<T> : SessionBase where T : new()
{
    private static readonly Object _padlock = new Object();

    private static string Key
    {
        get { return typeof(SessionBase<T>).FullName; }
    }

    public static T Current
    {
        get
        {
            var instance = HttpContext.Current.Session[Key] as T;

            if (instance == null)
            {
                lock (SessionBase<T>._padlock)
                {
                    if (instance == null)
                    {
                        HttpContext.Current.Session[Key] = instance = new T();
                    }
                }
            }

            return instance;
        }
    }

    public static void Clear()
    {
        var instance = HttpContext.Current.Session[Key] as T;
        if (instance != null)
        {
            lock (SessionBase<T>._padlock)
            {
                HttpContext.Current.Session[Key] = null;
            }
        }
    }

}

Now create an object (marking it as serializable helps)

[Serializable]
public QuestionCollection
{
  public QuestionCollection()
  {
    this.Questions = new List<Question>();
  }

  public List<Question> Questions { get; set; }
}

Now make the QuestionCollection strong typed QuestionCollectionSession

public QuestionCollectionSession : SessionBase<QuestionCollection>
{
}

Now you can use it like:

QuestionCollectionSession.Current.Questions.Add("Are you there?");

When you want to clear/remove it from session:

QuestionCollectionSession.Clear();

Upvotes: 3

Karl Anderson
Karl Anderson

Reputation: 34846

This is where the usage of var to infer the type bites you.

Explicitly state the type of QuestionList, like this:

List<sp_GetAllQuestionsResult> QuestionList = = myContext.sp_GetAllQuestions().ToList();
Session["QuestionsList"] = QuestionList;

Now it is obvious when you get the value out of Session what you need to cast it to, like this:

List<sp_GetAllQuestionsResult> QuestionList= Session["QuestionsList"] as List<sp_GetAllQuestionsResult>;

// Check if the list is null or not
if(QuestionList != null)
{
    // Safe to use list because it was found and successfully cast

}

Upvotes: 2

V.B.
V.B.

Reputation: 69

News newsList=new News();

Session["news"]=newsList;

when you wanna get session object;

News newList=Session["news"] as News;

You have to cast session .Use like that

Upvotes: 0

doogle
doogle

Reputation: 3406

Really depends on what type your QuestionsList represents, but if you assume QuestionList is a List<Question> then you'd want something like:

var QuestionList= Session["QuestionsList"] as List<Question>;

Upvotes: 0

AD.Net
AD.Net

Reputation: 13399

List<Question> QuestionList= Session["QuestionsList"] as List<Question>;
List<Question> QuestionList= (List<Question>) Session["QuestionsList"];

You have to have the same type of objects to be able to cast and it has to be serializable.

The first one will give you null if it can't convert and the 2nd option will crash.

Upvotes: 1

Related Questions