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

I am trying to work on an application where i am storing a string into Session like.

C#
Session["MyString"]


Further on the PageController Page.

I have code like below in Page Load.

C#
If(!Session.IsNewSession)
{
    if(user == null)
   {
     ////////////Do this...
    }

}
else
{
  if(Session["MyVariable"] !=null)     //Here I am always returning null :(
    {
     Delete();
     }
}


Can any body explains me that when the else part will be or should be executed?? and secondly why my
C#
Session["Variable"]
always getting null in return in else part?


Thanks :)
Posted
Comments
Ashish_Agrawal 20-Dec-13 6:46am    
Where are you storing value into your session?
VICK 20-Dec-13 7:40am    
when i entered a specific page, there i am calling a function n page load. and in that I have been storing that.

1 solution

Um. Lets have a look at that code, shall we?
C#
If(!Session.IsNewSession)
{
...
}
else
{
  if(Session["MyVariable"] !=null)     //Here I am always returning null :(
  {
...
  }
}
So, you only directly try to use the Session variables if this is a new Session? When teh variables will default to null? And you are surprised that they are null...
 
Share this answer
 
Comments
VICK 20-Dec-13 7:36am    
Due to bad english, perhaps i could not have understood completely.. but I think you are saying that ELSE will come into work when the Session will be a new one and all old variables/sessions etc will default to null.. and hence i am getting null...

RIGHT??? :(
OriginalGriff 20-Dec-13 8:00am    
Yes.
IsNewSession returns True for a new session, and "!" inverts that, so your else is only executed when you have a new (and hence empty) Session.
VICK 21-Dec-13 5:52am    
hmm. dnt have much experience on sessions. well it means if i made two sessions during application run, like session["a"] & session["b"], so if application remains to idle the main HTTp session state will be null and hence both of my created session variables will be null as well... RiGHT?
OriginalGriff 21-Dec-13 6:38am    
Yes. Remember, with seb sites, the Session is external to you application, and your application is "killed" as soon as the page data has finished going to the browser - and that means all your application data is gone as well unless you have stored it to the Session and / or cookies.
VICK 20-Dec-13 7:39am    
Well. my problem is that I have to delete a record from db based on an ID, which I am storing in Session["MyVaraible"].
and I have a page controller class which consist of the code like i posted above.
every page of the application is inherited from this controller class.
So if i leave my application idle and it times out. i have seen that else comes into play that time.. and becuase mystored ID is null. so I am unable to delete the related record from DB.

is there any possible solution for that.?

Will be really glad, if you could guide me in bit detail.

NOTE: Have already Googled and checked MSDN. but unable to understand quitely..

:(

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