?
Solved

Problem with casting Session state cached data

Posted on 2004-08-28
4
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
dnscg earned 2000 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 Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

765 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