Solved

Problem with casting Session state cached data

Posted on 2004-08-28
4
254 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now