3

ASP.NET: Is it possible to keep complex object such as List<object> is the session? If yes, how to do so? I'm trying to keep a list in the session, but I'm being told that I cannot convert that List to a string.

EDIT

[Serializable]
public class Client
{
        public string ClientType { get; set; }
        public string ClientName { get; set; }
        public string SubClientName { get; set; }
        public string Project { get; set; }
        public string Service { get; set; }
        public string Activity { get; set; }
}
List<Client> ListOfClients;
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {
        ListOfClients = new List<Client> { new Client()}; 
        Session["ListOfClients"] = ListOfClients;//This is where the error occurs
                                                 //Cannot convert List<Client> to string... 
     }  
}

There are many operations to execute, but the idea is to keep whatever is in the list of clients in the session.

Thanks for helping.

Richard77
  • 20,343
  • 46
  • 150
  • 252
  • 2
    I'm pretty sure anything that is `Serializable` can be stored in a session. Would you mind posting your code? – mclark1129 Aug 14 '12 at 14:58
  • 1
    The only thing I can think of is that Client does not implement `ISerializable` and is therefore not serializable for some reason. Trying adding the `[Serializable]` attribute to your class. – mclark1129 Aug 14 '12 at 15:13
  • I've added [serializable], but still get the same error, see the code I'm using – Richard77 Aug 14 '12 at 15:18
  • Post your full `Client` implementation, in case there is something inside it we're not seeing. – mclark1129 Aug 14 '12 at 15:23
  • There are just a bunch of string as the ones you see there, i.e. Address, street, ...see edits – Richard77 Aug 14 '12 at 15:27
  • I just tried this code verbatim in my own Web Application project. It worked great with no exception. – mclark1129 Aug 14 '12 at 15:38
  • Do you have some other thing in scope called Session? Like some Page member variable or similar? Or some own custom class/user control called Session? Does the error come at runtime or compile time? – user1429080 Aug 14 '12 at 15:48
  • the error happens at compile time. Also, besides there are some other sessions objects, but none of them has the same name. – Richard77 Aug 14 '12 at 15:55
  • 1
    Show Page directive of this page. It's placed at first line of aspx file – Yuriy Rozhovetskiy Aug 14 '12 at 18:40

3 Answers3

9

Yes, you can store any serializable object into a session variable. For example:

List<string> list;

Session["list"] = list;

Then to return the value:

List<string> list = (List<string>)Session["list"];
Community
  • 1
  • 1
Curtis
  • 101,612
  • 66
  • 270
  • 352
6

You can store anything in the session object as long as session state is in InProc mode.

Otherwise what you store has to be serialisable.

Note that the type of what you store is object, so you cast the reference that you get back:

ListOfClients = Session["ListOfClients"] as List<Client>;
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Since you wrote in a comment that the error happens when compiling as opposed to at runtime, I suspect that you have some other object called Session that masks the Page.Session.

Try to hover your mouse over the Session text in Visual Studio. The tooltip should show you that Session is of type

System.Web.SessionState.HttpSessionState

If it's showing something else, you need to search both your markup (.aspx file) and code behind file to see if you have declared something else with the name/id Session and then change that name/id.

user1429080
  • 9,086
  • 4
  • 31
  • 54