6

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.

Brian Hooper
  • 21,544
  • 24
  • 88
  • 139
  • Iserializable does not matter as you are not writing to disk. Is your array really big, or did you take very long to retrieve the data? Maybe the server was overloaded. Seems like a very strange problem. – MrFox Dec 14 '12 at 15:53
  • @MrFox, the arrays have a dozen or so elements and I am working on my development machine, which I am sorry to say is not overloaded. – Brian Hooper Dec 14 '12 at 16:00
  • What is your code for retrieving the array from the session? You said it isn't working -- but what is it? – Michael Paulukonis Dec 14 '12 at 16:05
  • Ah, you were never storing nor retrieving the array directly. That's what was throwing me. Still does, looking at the above. – Michael Paulukonis Dec 14 '12 at 16:43

3 Answers3

7

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"];
Azhar Khorasany
  • 2,712
  • 16
  • 20
3

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.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thanks for the tip. I will check, but I doubt I'm clearing the arrays by accident. This problem affects all the arrays of classes. The native types, the arrays of native types, the structures and the arrays of structures are being returned as I would expect, but all the arrays of classes are exhibiting this problem. – Brian Hooper Dec 14 '12 at 15:49
1

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)

Sean
  • 2,990
  • 1
  • 21
  • 31