Solved

Problem with casting Session state cached data

Posted on 2004-08-28
4
275 Views
Last Modified: 2008-03-17

Hello, new to .Net (using c#). I've created a stronly typed collection, which I've marked as serializable, so that I can pass it into the session cache and get it back out again for either displaying or updating at a later point. Problem is once I've put it into the session I can't seem to get it back out again because I'm getting the error "Specified cast is not valid" on this line:

DataSet ds = (DataSet)Session["myCahedData"];

I've decorated both my class and another other class, which acts as a placeholder for the collection of the aforemetioned  objects, as serializable. I can use these just fine in the initial build of this collection, display the contents by binding it to a grid, as well as putting it into the session object, like so:

Session["myCachedData"] = myCol.GetArrayList();

The error leads be to belive that the collection isn't serialized, but that's not the case. Perhaps I'm not putting this into the session state correctly in the first place? this is a time sensitve issue. thanks in advance!!
0
Comment
Question by:animated405
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
dnscg earned 500 total points
ID: 11923722
Hi animated405 -

I'm not 100% certain of your container class implementation, but if your return object is of type ArrayList (by looking at your code snippit), keep in mind that the ArrayList class is already serializable - so there is no need for additional decorations.

Also, when inserting objects into the Session object, you should first cast it as an object.

Try this bit of code to insert it into Session cache (modifying it for your needs), and see if it works for you:

ArrayList alTemp = myCol.GetArrayList();
Session["myCachedData"] = (object)alTemp;

And to retrieve it (you had the right idea with your DataSet example):
ArrayList myArrayList = (ArrayList)Session["myCahedData"];

Note:  I don't believe that there is a silver bullet direct cast from an ArrayList to a DataSet (f you were trying to cast an ArrayList as a DataSet).  You may be able to create a DataTable by itterating through your array list, and then insert that DataTable into a DataDet.  Again, I wasn't sure of what "end object" type you want to get out of the Session cache - but the ArrayList obect example should work fine.

Good luck with it :-)
dnscg (Aki)


0
 

Author Comment

by:animated405
ID: 11923826

thanks for the response. I tried that and was able to atleast get the page to load without breaking, of couse I didn't actually try to do anything with it yet. I've been trying to learn this f---- .Net for like two weeks know and I still don't get the most firggin basic stuff, it's driving me absolutely mad.

so, do like you suggest and the page loads atleast:
ArrayList myArrayList = (ArrayList)Session["myCachedData"];

Now I want to iterate through that and put into a dataset, of course I'm getting specified cast not valid every single time I try to do something.

foreach(string sName in myArrayList)
{
Response.Write("Name: " + sName);
}

why can't I do this simple foreach with that above???
0
 

Author Comment

by:animated405
ID: 11924268

I dont' know what I was doing wrong but I got it working, was very frustrated...thanks for your help!!
0
 
LVL 1

Expert Comment

by:dnscg
ID: 11924362
No problem.  Glad to hear that you worked through the issue.
Don't be frustrated with .NET ... it is a steep learning curve, but once you get over the hump ... well, it's like heaven :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question