I am trying to solve a problem where the session id seems to clearing mysteriously during postback. I am sure that the value is being set and there is no other place in my code where i am clearing that session. Also, I am storing the value of the session id in my page's viewstate. During the postback the viewstate is empty which essentially means that when the value was assigned to viewstate the session variable was null. Is it possible that during code execution, session object is cleared because of timeout?
So lets say if i have following code.
if (session["id"] == null) :line1
{ :line2
session["id"] = // Generate some unique id :line3
} :line4
viewstate["id"] = session["id"]; :line5
is it be theoretically possible that even though session["id"] is not null in line1 it is null on line5 because of time out.
Nothing. Session will not expire when uses closes the browser. As http is stateless, the server is not seeing what happens on client side.
When the session expires, or session timeout occurs, the Session_End event in global. asax is raised (except when session is handled by the DB) and the session collection is finally cleared. If any objects are NOT holding a reference to any of values in the session collection, then GC will collect it.
If your Internet connection is unstable, periodically disconnecting and reconnecting, it can cause a website session to expire. When the Internet connection is lost the website connection can be terminated, resulting in a session expired message if you try to access any page after the Internet reconnects.
To preserve web server's resources, session expires after certain time of inactivity. Inactivity is considered as time between two consecutive requests from same visitor. By default, ASP.NET session will expire after 20 minutes if visitor doesn't visit any new page.
I'm gonna have to say no. I just made a site and set the session timeout to 1 (minute)
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState timeout="1"></sessionState>
</system.web>
Then added a web page with this in the page load
protected void Page_Load(object sender, EventArgs e)
{
Session["Test"] = "Tester";
//Should be longer than the 1 minute session timeout
Thread.Sleep(120001);
Response.Write(String.Format("Session[\"Test\"] = {0}", Session["Test"]));
}
I tested on the Cassini VS debugger, and on IIS 7 asp.net 4 and in every test the page loads with Session["Test"] = Tester. I also tried recycling the application pool manually during the sleep and got the same results.
this is my code and it is working properly...
protected void Page_Load(object sender, EventArgs e)
{
if (Session["id"] == null)
{
Session["id"] = "abc";
}
ViewState["id"] = Session["id"];
Label1.Text = ViewState["id"].ToString();
ViewState["id"] = Session["id"].ToString();
Label1.Text += ViewState["id"].ToString();
}
Change "session" to "Session" and "viewstate" to "ViewState"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With