Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi !!!

i have a problem. i have a deployed a website. When user enters and add product to the cart they are added to a session variable which i have added through a class file.

C#
static ShoppingCart()
   {
       // If the cart is not in the session, create one and put it there
       // Otherwise, get it from the session
       if (HttpContext.Current.Session["ShoppingCart"] == null)
       {
           Instance = new ShoppingCart();
           Instance.Items = new List<CartItem>();
           HttpContext.Current.Session["ShoppingCart"] = Instance;
       }
       else
       {
           Instance = (ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
       }
   }



now if user closes the website an another user open the website, my cart is showing the objects added by the previous user.

how to clear the cart items on application start or application end ???
Visit www.woodworld.co
Posted
Updated 31-Jan-12 0:37am
v3

Hi,
i wish you have Globlel.asax file.
In that file
C#
Session_OnEnd
method.
if you log out from the site
C#
Session_OnEnd
fired and Clear Session using
C#
Session.Abandon(); 
method.
But if you close the browser it will not fired and use following articles to solve that problem.

http://aspalliance.com/1294_CodeSnip_Handle_Browser_Close_Event_on_the_ServerSide.all[^]

http://zeeshanumardotnet.blogspot.com/2011/01/how-to-end-session-when-browser-is.html[^]

http://forums.asp.net/t/1116238.aspx/2/10[^]

do not hesitate to mark this as answer if this helps to you..
 
Share this answer
 
Comments
abhishekagrwl25 2-Feb-12 2:31am    
tried all the solutions provided by you but they are just not working. :'(

the problem remains the same.
I can clear the session on logout but it's not getting abandon or cleared on closing the browser without logout
For Clearing Session Data use
C#
Session.Abondon();
Session.Clear();

Session is a user specefic object. If you stored a data in Session then the data is only for the that particular user of that terminal.
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 1-Feb-12 8:59am    
Added pre tag
abhishekagrwl25 2-Feb-12 2:30am    
i have made session in a class file and the class file will be accessed by all the users. don have a clue how to solve this problem... :'(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900