Reputation: 1245
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.
Upvotes: 8
Views: 1450
Reputation: 1527
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"
Upvotes: 1
Reputation: 1728
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.
Upvotes: 3
Reputation: 815
No because you would never get to line 5 if a timeout occurred. The program, service, whatever, would stop running.
Upvotes: 0