Using Session state to store objects

Last post 03-02-2006 4:27 AM by StrongTypes. 1 replies.

Sort Posts:

  • Using Session state to store objects

    03-02-2006, 4:18 AM
    Hi

    I want to save an object, and make it available over several pages.  So far it seems the best way for me to do this is to use sessions.  However I can't find a way to store an object for the duration of a session.  Below is some of the code

    Fragment of Method 1

    SearchResultCollection ownerResults = Code to return SearchResultCollection

    Session["ownerResults"] = ownerResults;

    Fragment of Method 2

    SearchResultCollection ownerResults = Session["ownerResults"];

    However, I get the following error.

    Error 1 Cannot implicitly convert type 'object' to 'System.DirectoryServices.SearchResultCollection'. An explicit conversion exists (are you missing a cast?)

    My question is, how can I store an object, to be used by the same page, or different page in the same session?

     

    Thanks

    Ben

  • Re: Using Session state to store objects

    03-02-2006, 4:27 AM
    • All-Star
      30,697 point All-Star
    • StrongTypes
    • Member since 12-13-2005, 11:21 AM
    • California
    • Posts 6,007
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    You'll need to do something like this:

    SearchResultCollection ownerResults = Session["ownerResults"] as SearchResultCollection;

    if (ownerResults != null)
    {
       // Code here
    }

    HTH,
    Ryan

    Ryan Olshan
    ASPInsider | Microsoft MVP, ASP.NET
    http://ryanolshan.com

    How to ask a question
Page 1 of 1 (2 items)