Brian Hooper
Brian Hooper

Reputation: 22074

How do I save an array of objects in a C# ASP.Net Sesssion?

I have in my C# ASP.Net program an array of objects that are filled in during a postback and I wish to recover them during the next postback. To this end I include the array declaration in the Default class definition which is saved thus:-

this.Session["This"] = this;

and recovered:-

Default saved_stuff = (Default) this.Session["This"];

This works fine for everything apart from:-

MyClass [] my_array;

When recovered, saved_stuff.my_array is always null.

MyClass is defined like this:-

public MyClass : ISerializable
{
    private string some_string;
    private double some_double;
    // some more simple members
    // Some getters and setters
    // ISerializable Implementation
}

I have tried making MyClass implement ISerializable but that doesn't make any difference. Does anyone know what I should be doing?

Edit to answer @Michael's question, I'm then doing things like...

for (int i = 0; i <= saved_stuff.my_array.GetUpperBound(0); i++)
{
    // blah blah
}

which is failing with "object reference is not set to an instance of an object". All the other member variables of Default are visible in saved_stuff when in debug.

Upvotes: 6

Views: 9685

Answers (3)

D Stanley
D Stanley

Reputation: 152624

According to MSDN, if you are storing session state in memory (the default), then it does not need to be serializable

When you use a session-state mode other than InProc, the session-variable type must be either a primitive .NET type or serializable. This is because the session-variable value is stored in an external data store.

I suspect something else is clearing out the array. Remember that

this.Session["This"] = this;

when using in-memory session state does not create a copy of your page object, so any changes you make to the page object ("this") will also be made to the "saved" object. So if you set this._saved_stuff = null then the saved object's property will be null as well.

I would not try to store the page in session, rather pick and choose what values you want to save and save them explicitly.

In addition, if you want the values to be recoverable in the next postback, you may want to choose ViewState instead. Using Session does run some risk that the session will be destroyed between postbacks. It may be rare, but is it a consideraion.

Upvotes: 3

Azhar Khorasany
Azhar Khorasany

Reputation: 2709

You have to store your array data in its own session object:

MyClass[] my_array = //Something

Session["ArrayData"] = my_array;

Then retrieve it as:

var arrayData = (MyClass[])Session["ArrayData"];

Upvotes: 7

Sean
Sean

Reputation: 3052

First of all you should know that in Asp.net Web application there are three type of Session State 1. in process 2. State Server 3. In Database

you can save the object directly into the session if the session state is in process (which is defalut), or you need mark your object to be serializable, that allow you to save the object into the state server or in database (set in the config file)

Upvotes: 1

Related Questions