Session Variables lost after Response.Redirect()

Hi gurus, 

I encouter lost session variables trouble when Response.Redirect() to another aspx page.
I set 19 Session variables before redirecting. However, the redirected page's session contains only One variable.

The very weir sympton is: If I restart IIS, only for the first IE encounter, the redirected page DOES show all 19 session variables.
Once I close IE, any further IE or IE from any other machine's only shows ONE session variable.

I can also reproduce such symptons in VS2005 development environment.

Can someone tell me what I have missed?   Thanks!

Here is my very straight-forward code pieces:

Page1.aspx C# Code behind:  (I assign 19 session variables before redirecting to Page2.aspx)
    protected void btnTest1_Click(object sender, EventArgs e)
        // set session variables
        this.Session["_Mode"] = "New";
        this.Session["_Action"] = ActionParam;
        . . .
        // at end Session contains 19 session variables      

Page2.aspx C# code behind: (lblDebug shows all session variables)
    protected void Page_Load(object sender, EventArgs e)
        foreach (string sName in Session.Contents)
            this.lblDebug.Text += sName + " = ";
            if (this.Session[sName] != null)
            { this.lblDebug.Text += this.Session[sName].ToString() + " <br/> "; }
            { this.lblDebug.Text +=  "null <br/> "; }

My Web.config contains: (Cookie is enabled in all machines)
   . . .
   <sessionState mode="InProc" cookieless="false" timeout="40" />

Here are strange symptons:
a) In IIS environment:
    i) If I restart IIS, Pages2.aspx WORKS, but only for the first encounter, and shows all 19 session variables.
    ii) If I close IE and launch a new IE, Pages2 only shows ONE session variable.

b) In VS2005 Development environment:
    i) If I launch VS2005 from scratch and run the web project, Page2.aspx WORKS and shows all 19 session variables.
    ii) However, if I close the browser and run he web project again, Pages2.aspx only shows ONE session variable.
    iii) When I debug the 2nd run:
         - In Page1.aspx  Session.Count = 19 before Response.Redirect() line
         - In Page2.aspx  Session.Count = 1  in Page_Load()

Please help...

4/19/2008 11:58:18 PM 8807 articles. 0 followers. Follow

7 Replies

Similar Articles

[PageSpeed] 33
Get it on Google Play
Get it on Apple App Store


That should not even compile! should be someting like


My Blog
Please click on 'Mark as Answer' if this post answered your question!
4/20/2008 1:02:01 AM


use this

Response.Redirect("~/Page2.aspx", false);

this will not end the current thread ,and so the session variables can be set ...


Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

4/20/2008 9:41:36 AM

Sorry, that was my typo since I replaced the actual page name . I did code 


4/20/2008 12:45:58 PM

The link you mentioned is indeed a great article that explains the root cause of the problem. 

However, I tried the overload method
   Response.Redirect("~/Page2.aspx", false);
It DOES not work for me. I am still not getting the same Session State.

In the linked blog you mentioned, I saw mixed response. Some said the oveload method work, and others said otherwise. I do not see a solid answer as exactly how to make it work. Can you shed morel ight?

Page2.aspx does works if I use
But, this approach causes other issues for me in my real page and it is not the solution I prefer.  

I notices that in Page2.aspx, the Session.SessionID is changed (that proves Pages2.aspx was assigned a fresh new Session State.)   In my post, I said I got onle ONE session variable in Page2.aspx. I was wrong, Page2.aspx actually get NO session variable to begin with.




4/20/2008 1:08:45 PM


I can't find any clue !

did you have any code that modify some files or applciation directory ? are you modifying  the web.config before redirecting to the other page ?



Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

4/24/2008 9:33:31 PM

Hi Jan,

Most of the browsers will accept maximum 10 to 15 session variables or cookies. If U try to use nore than it previous contents will be lost.

You can use following options

1. pass variables by querystring (dont' pass sensitive data like password. in such a case session is recommended.

2. use ViewState variable

3. use Request.Forms variable


  • Check whether session state is enabled in IIS
  • go to IIS, click Your Virtual directory . go to properties
  • in the dialog go to Virtual directory Tab and click configuration button
  • A new dialog will be shown
  • go to Options Tab
  • check the Enable session state
  • refresh it
4/25/2008 2:41:46 AM

HI jansuan,
I realy suprised by  the question you asked. But important thing is  to be noted that there are good amount of session variables which not all browsers supports or fail to support.

Better use query string and pass the value from on page to the others. But if u really want to use the Session variables the one thing u can do.

Make a BE and use all the 19 variables as a property to this BE and use this single BE into ur session. If u have to assign something then typecast it inot that BE and add value to by BE.Property name=<value>

and get it in the same way,


"Mark as Answer" on the post that helped you.

Imfinity India Pte Ltd.
4/25/2008 7:33:06 AM

Similar Artilces:

Response.Redirect with Session Variable vs. Global Variable -- Best Practices?
Hi,I have a simple contact form and when the user presses the submit button an email is sent and  I have a response.redirect to "emailSent.aspx" where it says "Your request has been received....etc.".  I have the submit button in a try/catch block.  What I want to do is a "emailFailed.aspx" where it says "Email problem, etc." but I also want to carry over the ex.message text.  I want to pass the ex.error text to the "emailFailed.aspx" page so on pageLoad I can have a label with something like:labelMessage.Text = "There was a problem with your email.  The error is " +...

session lost with response.redirect
hi.I have an 2003 web project with is simple.when user logs in through login.aspx, his login information is being kept in session variables. then with response.redirect he goes to main page. here there is a session control in page load if his session expired. but session gets lost immediately after he logins and goes to the main page. what could be causing this? thanks. Hi , take look at this article Amrit hi thanks for your answer...

Session variable lost in redirect
I am having a problem with loosing a session variable after I redirect to another page. Here is my code:login.aspx: if(sAuthorized != "-1" && sAuthorized != "-2") { Session["user"] = sAuthorized; Response.Redirect("index.aspx"); }index.aspx:private void Page_Load(object sender, System.EventArgs e) {    CheckLogin();    string sUserID = Session["user"].ToString();    DataSet ds = SqlHelper.ExecuteDataset(DataAccessLayer.ULTConn(), "sp_get_users_courses",    new SqlParameter("@user", SqlDbType.VarChar, 20).Value = ...

Losing session variables after response.redirect
I'm using StateServer to manage my sessions. <sessionState mode="StateServer" stateConnectionString="tcpip=" /> I then set the session values as follows on one page: Session("UserID") = dbReader("UserID")Session("FirstName") = dbReader("FirstName")Response.Redirect("~/member/Default.aspx") When it redirects to the next page, the session values are blank (null). I'm not sure at all what's happening. The IIS settings are fine as well. XP Pro, ASP.NET 2.0, IIS 5. There are too many protential reasons for session loss, and here is a newly post article on this, whi...

Session state lost when url is an redirect
Hi, I have a website ( and created a implementation of user validation (just a validate with database and save it into the session). The user data will be saved in Session, to make this information available through the application. This works fine. However, there exists also a website (, but this only a redirect to "mysite1". This redirect will keep the url of "mysite2" in the address bar. Now the part i don't get: When login through "mysite2" the userdata isn't persisted. Could anyone tell me why the second website give these problems? tx, RamR0m d...

Session variables lost after a redirect page
Hi all , I have encounter a session variable lost after a redirect page when i set the cookiesless=false in web.config. This happen when u i tried to access the application from another pc while my application reside in another web server. therefore in order to solve this problem i tried to change in the web.config file by setting the cookieless =true than it works. <sessionState mode="InProc" stateConnectionString="tcpip=" sqlConnectionString="data source=;Trusted_Connection=yes" cookieless="true" timeout="20"/> how can i have this prob...

Session State variable missing after Redirect
When an error occurs, I set a session variable so that after redirecting to an error page, a detailed explanation can appear.  The variable (a string) is gone by the time I reach Page_Load in the target page.  I've checked and it is the same session ID. Any ideas? Are Sessions enabled? Are you forwarding to a page outside of you application?View My Blog Download My URL Rewriter and Reverse ProxyOnly $9.95/month, ASP.NET, 2GB & SQL 2005...

session variable gets empty after response.redirect
 Hi,I am not getting why following things happening.. 1. I have declared session variable in global.asax2. Session("postrentattempt") = ""3. In the page load of post rent page I am writing           Session("postrentattempt") = "1"            Response.Redirect("sellerlogin.aspx")  4. Now, in sellerlogin, I am checking if             Session("postrentattempt") =...

State management upon session variable changes
I noticed that whenever a session variable is changed...the application knows about it and suddenly restarts session. My question is does restart "ALL" sessions or only the active sessions for which the variables changed? Imagine a "multi-user" application all with different states and an administration panel that lets you change session variables.  In this case, my question has to do with the question that if Company X changes his session variables, will only Company X's sessions be reset or will all "sessions" be reset?  Hope that makes sense. ...

Query string and session variables lost after redirect
Hi, I'm trying to redirect a user form one application to another by using response.redirect(). I need to pass a returnURL so I'm doing this through a query string: In myapp1 I call: Response.redirect(http://localhost/myapp2/logon.aspx?ReturnUrl=, true) When debugging the myapp2, in page_load of logon.aspx.cs I've lost the query string AND some session variables I stored. The session ID is the same.... Any idea? Any configuration to be set in web.config? Thanks a lot, Claudio...

Session variables vs. Session States
What is the difference and in what situations are they used. Thanks, Mattypee A session state (the state of your session) holds your session variables. If your session state expires (due to inactivity) then you lose your session variables...and any other info stored for that session. If you need to store USER specific information within a web app, then session variables are one(of several) ways to do it. MajorCatsMajorCats Is there any code that you could show that would give an example? Thanks, Mattypee example of what? reading and writing session variables? ...

Opinions on Viewstate vs. Session variable for state management on dataset
I came across many sample codes that using ViewState(myDataSet) and Session("myDataSet"). Which is a better way to use to keep the session around. What are the pros and cons? Anybody can share with your experience or thoughts? Thanks, teresa Take a loot at this article: State Management in Web Forms Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you!...

How to Debug Response.Redirect losing session variable, when upload file
HELP! I have a page where users can enter text and optionally upload an image. To upload the image, the page contains an image file upload usercontrol I created. The control displays the image after loading and stores the image information to the database and creates an image ID. Then the user can submit the entire page which just saves the user's text and the image ID to the database. In addition, I create a user account for the user and store their new id in a session variable ("LoginID"). When saved, the page does a response.redirect to the next data input page. Excluding the first pag...

Lost session variables on redirect after cross-domain httppost request
Hi all, I have an asp page with an httppost request to bypass login for another .net page on another domain.  This works fine and returns the results as expected.  I am having difficulty when navigating away from the resulting page as the post request I have made is simply response.write of the xml returned so the page stays on the old domain without a redirect to the new site.  Therefore any links I have on the new page do not work without using the fullpath to the page, same goes for any buttons which do postback as the page does not exist...