0

In my ASP.Net application, I'm storing a string collection in a Session variable. It was working fine but now I want to change session settings from in-proc to Out Proc. Now I'm having an issue that "Non-Serializable Object Stored in Session". How can I serialize the Sting Collection object before assigning it to a session variable? Please help.

Session["SelectedItems"] =customerGroupsList.SelectedItemsCollection;

I want to change above line of code.

rinesh
  • 493
  • 1
  • 8
  • 26
  • It's obvious that `SelectedItemsCollection` is non-serializable, you'll have to create a POCO for this, and serialize to/from it most likely. Alternatively, you may try serializing with JSON.NET (whatever generic JSON serializer you like) and storing the serialized string to the session-state variable. – Der Kommissar Jul 24 '17 at 16:20
  • What about Instead of assigning StringCollection types to the session, derive a class out of StringCollection, make it serializable and use that class object to assign to the session. is there any issue? – rinesh Jul 25 '17 at 07:54
  • Are you sure `SelectedItemsCollection` is actually a [`StringCollection`](https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection(v=vs.110).aspx)? Because `StringCollection` is in fact marked with `[SerializableAttribute]` according to the documentation. Possibly it's some internal subclass, or something else entirely? – dbc Jul 25 '17 at 21:13
  • Its a StringCollection only. `public StringCollection SelectedItemsCollection { get { return dataGridController.SelectedCheckBoxItemsCollection; } }` – rinesh Jul 26 '17 at 09:50

1 Answers1

0

If it's just really a string collection object, then you case use the code below to serialize it and store it as String:

        Session["SelectedItems"] = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(customerGroupsList.SelectedItemsCollection);
kots
  • 465
  • 2
  • 7
  • This will make a lot of changes in my application.It's required to deserialize every time I use that session variable. Is it possible to do like this? Instead of assigning StringCollection types to session, derive a class out of StringCollection, make it serializable and use that class object to assign to the session – rinesh Jul 25 '17 at 07:52