Solved

Problem with casting Session state cached data

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

10 Experts available now in Live!

Get 1:1 Help Now