Showing posts with label arraylist. Show all posts
Showing posts with label arraylist. Show all posts

Thursday, March 29, 2012

Pass Object from one page to another

Hi, I have one page that has several methods that populate an ArrayList with data. I want to be able to pass this populated ArrayList to another page on the site. Is there any way that this can be done? I know values can be passed using the QueryString, but I need the whole ArrayList object to be passed.

Any help would be greatly appreciated.
Thanks

You can store it in a session variable:http://msdn2.microsoft.com/en-us/library/ms178581.aspx

From the page:

By default, session variables can be any valid .NET type. For example, the following code example stores anArrayListof values in a session variable named "StockPicks." Note that the valuereturned by the "StockPicks" session variable must be cast as theappropriate type upon retrieval from theSessionStateItemCollection.


1) You can keep the arraylist in the session object and can access the session object into another page.

2) You can make serializable class file and make the public object. By using context handler you can access the public object into another page.

Regards,

Suhas


Suhas.Chitade:

2) You can make serializable class file and make the public object. By using context handler you can access the public object into another page.

I've never tried this approach before. Would it mean that the same object is available to all users? Also, how long would the object persist?

Saturday, March 24, 2012

Passing (and retrieving) an ArrayList from a Session Var.

I am attempting to pass an ArrayList of objects to a Session Variable for retrieval on another page. Unfortunately, whenever I PostBack the page I get the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 49: objCart.SKU="111"
Line 50: objCart.Quantity=intQuantity
Line 51: aryCart.Add(objCart)
Line 52:
Line 53: Session("Cart")=aryCart

I have the Session("Cart") loading in a 'Not IsPostBack' conditional statement in the PageLoad and it works fine. But as soon as I try loading Session("Cart") into any PostBack situation (either in the PageLoad or any other function) I get the error above. I'm not sure why Session("Cart") only works for 'NotIsPostBack' situations. Is this just a problem related to ArrayLists? Any help would be greatly appreciated.are you creating a NEW objCart and/or a NEW aryCart?
Yes.
To anyone who is reading this thread, I've figured out the solution to this problem. When writing data to a Session variable from an ArrayList or an Array for the first time, the Session must first be initialized, otherwise it returns a Null value and you get the error I was getting. I solved this by using the IsNewSession method witihin a conditional statement to check if the Session was new or not. If it's new you cannot read from the Session variable, you may only write to it. If it's not new you can read from it or write to it.