Solved

Problem with casting Session state cached data

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

773 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