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      
        Response.Redirect(~/Page2.aspx);
    }
 

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/> "; }
            else
            { this.lblDebug.Text +=  "null <br/> "; }
        }
    }

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


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...

0
Jansuan
4/19/2008 11:58:18 PM
asp.net.state-management 8807 articles. 0 followers. Follow

7 Replies
4751 Views

Similar Articles

[PageSpeed] 2

Jansuan:
Response.Redirect(~/Page2.aspx);

That should not even compile! should be someting like

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


Cheers
Al
My Blog
MapStats.NET
Please click on 'Mark as Answer' if this post answered your question!
0
albertpascual
4/20/2008 1:02:01 AM

Hi

use this

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

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

http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx


Regards,

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!

0
anas
4/20/2008 9:41:36 AM

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

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

0
Jansuan
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
   Server.Transfer("~/Page2.aspx");
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.

Regards,

Jansuan

 

0
Jansuan
4/20/2008 1:08:45 PM

Hi

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 ?

 


Regards,

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!

0
anas
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

Important:

  • 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
0
Shakthishyamala
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.

Chandan,
Imfinity India Pte Ltd.
0
chandan
4/25/2008 7:33:06 AM
Reply:

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 asp.net 2003 web project with vb.net.problem 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 http://weblogs.asp.net/bleroy/archive/2004/08/03/Don_2700_t-redirect-after-setting-a-Session-variable-_2800_or-do-it-right_2900_.aspx 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=127.0.0.1:1702" /> 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 (www.mysite1.com) 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 (www.mysite2.com), 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 asp.net application from another pc while my asp.net 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=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;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=http://www.mysite.com, 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 http://www.c-sharpcorner.com/Code/2004/Feb/StateManagementInWebForms.aspDarrell 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...

Web resources about - Session Variables lost after Response.Redirect() - asp.net.state-management

Variable - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Westpac interest rate rise is one of many variable loan rate rises
WESTPAC is not alone in hitting its owner-occupier customers with a surprise interest rate rise, as dozens of other variable rate home loans ...

Airbnb upgrades variable pricing tool for hosts
Online home rental group Airbnb is rolling out an upgraded variable pricing tool that automatically raises or cuts room rates based on supply ...

Commonwealth Bank raises variable home loan rates
Home owners' mortgage costs are set to rise by hundreds of dollars a year, after the Commonwealth Bank became the latest bank to hike interest ...

CBA lifts variable home loan rates to offset costs associated with stricter capital requirements
The Commonwealth Bank to lift its home loan interest rates independently of the Reserve Bank to partially offset costs associated with stricter ...

Washington Square Signs Spielbergs, Choi Joins Variable and More
... films and an episode of the HBO comedy series "Girls," on which he plays the role of Ray Ploshansky. New York-based creative collective Variable ...

YouTube v10.32 Tweaks Some Interface Elements And Includes Hints About Virtual Reality Content And Variable ...
Forget Update Wednesday! Monday is the new big day. Not only were we treated to the latest M preview release and the official Marshmallow name, ...

"You have to ignore many variables to think women are paid less than men. California is happy to try." ...
Writes Sarah Ketterer in The Wall Street Journal in "The ‘Wage Gap’ Myth That Won’t Die" (which you can get to without subscribing if you Google ...

Staley: 'A lot of different variables...been a hard year'
... 41° Navigation Home Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Staley: 'A lot of different variables...been ...

Expert Reveals the Latest Variable Capacity Technology
Learn about gForce Ultra CRAC equipment, the latest variable capacity technology to lower energy usage and increase reliability.

Resources last updated: 1/22/2016 6:34:41 AM