Question : C# - Error retrieving array from session state.

I'm storing an array of [serializable] participant objects to the Session object as such:


1:
2:
3:
4:
 _p = new List<participant>();
 _p.Add(new participant());
 Session["participants"] = _p.ToArray<participant>();


I'm retrieving the array as such:

1:
2:
3:
participant[] _array = (participant[])Session["participants"];
_p = new List<participant>(_array);



Everything works fine 90% of the time, however in the other 10% i get a very unclear error:

Unable to cast object of type 'participant[]' to type 'participant[]'


The error occurs on the first line of code provided for retrieving above.  The watch window indicates that session["participants"] is not null, and does indeed appear to contain a valid array with the expected item count.

Any help would be much appreciated.

Answer : C# - Error retrieving array from session state.


Even if the types differ only by attributes, still they are different types...have you added/removed any attribute recently...you have mentioned participant type has [serializable] attribute...is there a chance these objects are deserialized from a store which was saved with participant class before any change...and now you are trying to deserialize it to the class after the change (in attribute)...
Random Solutions  
 
programming4us programming4us