I have added two user object and then tried to store those in Session. I tried to get those from the sam aspx page first. It works fine there.
Then i tried to get that from another page using response.redirect. in that page i have used same code but it's (for List key) not giving same output, though for single "Name" key it's working well.
HttpSessionStateSample.Entity.
User
userObj = new HttpSessionStateSample.Entity.User();
there is no problem with string type. problem is when i am stroing a list in Session. then after redirecting to another page i am loosing the list value stored in session from the previous page. i have searched for this and one hints got from many sites
that it may be a case of Response.Redirect. Some one suggested to use Server.Transfer. I have tried to use Server.Transfer but it's not give me permission to compile it well.
From the default page i have added two item in session - a string 'name', a list item 'profileinfo'.
at the default page it get the list value and string value, here the 'profileinfo' key displays item count = 2.
But when it redirects to 'WebProfile' page i get the two key item name - 'name' and 'profileinfo'. the 'name' key contains it's value but the 'profileinfo' key
displays item count = 0;
At last i have got one possible solution for my problem. What i have done is - i have taken the objects as static (1) and then comment out (2) the lines which was clearing the list items after inserting them into Session["PropertyInfo"].
I don't think this is the best or even the only solution. There may have some technical issues involved with such cases. Still looking
for the logic behind it, because the why commenting these lines displaying correct list items (count=2) in both of the pages.
Ouch. Using static variables is a big no-no and a sure way to run into thread safety issues. Session is designed to deal with this. Working around a problem by re-implementing the standard component is a very bad idea. Jsut use the overload to the redirect
API that doesn't terminate the thread.
Bertrand
----
This posting is provided "AS IS" with no warranties, and confers no rights.
labshasan
Member
6 Points
4 Posts
Session value lost when page redirects to another aspx page
Sep 07, 2011 01:02 PM|LINK
I have added two user object and then tried to store those in Session. I tried to get those from the sam aspx page first. It works fine there.
Then i tried to get that from another page using response.redirect. in that page i have used same code but it's (for List key) not giving same output, though for single "Name" key it's working well.
HttpSessionStateSample.Entity.
User userObj = new HttpSessionStateSample.Entity.User();
List<HttpSessionStateSample.Entity.User> pProvider = new List<HttpSessionStateSample.Entity.User>();
userObj.UserName =
"Hasan";
pProvider.Add(userObj);
userObj =
null;
userObj =
new HttpSessionStateSample.Entity.User();
userObj.UserName =
"Arif";
pProvider.Add(userObj);
userObj =
null;
try
{
HttpSessionStateSample.Utility.
SessionManager.SetItemToSession("Name", "Hasan");
HttpSessionStateSample.Utility.
SessionManager.SetProfileInfo(pProvider);
Response.Redirect(
"~/WebProfile.aspx");
another page code:
string
name = HttpSessionStateSample.Utility.SessionManager.GetItemFromSession("Name").ToString();
List<HttpSessionStateSample.Entity.User> profileList = Utility.SessionManager.GetProfileInfo();
reza141414
Participant
1565 Points
472 Posts
Re: Session value lost when page redirects to another aspx page
Sep 07, 2011 03:22 PM|LINK
hi..
use this way :
and restore the session value like this:
dont forget to mark as answerd...
labshasan
Member
6 Points
4 Posts
Re: Session value lost when page redirects to another aspx page
Sep 08, 2011 06:53 AM|LINK
there is no problem with string type. problem is when i am stroing a list in Session. then after redirecting to another page i am loosing the list value stored in session from the previous page. i have searched for this and one hints got from many sites that it may be a case of Response.Redirect. Some one suggested to use Server.Transfer. I have tried to use Server.Transfer but it's not give me permission to compile it well.
bhagirath
Member
378 Points
77 Posts
Re: Session value lost when page redirects to another aspx page
Sep 08, 2011 06:55 AM|LINK
Hello,
Make sure your are redirecting on same server(URL).
salman behera
All-Star
26645 Points
5066 Posts
Re: Session value lost when page redirects to another aspx page
Sep 08, 2011 07:39 AM|LINK
get some idea here.....
http://www.dotnetbips.com/articles/c585b4d3-93c5-4c66-9d49-8e1946f4d311.aspx
http://forums.asp.net/t/1223291.aspx
http://msdn.microsoft.com/en-us/library/6c3yckfw.aspx
Thanks....
Sincerely,
Salman
labshasan
Member
6 Points
4 Posts
Re: Session value lost when page redirects to another aspx page
Sep 08, 2011 09:47 AM|LINK
I have added code like:
To Add:
System.Web.HttpContext.Current.Session.Add(key, obj);
To Get:
return System.Web.HttpContext.Current.Session[key];
From the default page i have added two item in session - a string 'name', a list item 'profileinfo'.
at the default page it get the list value and string value, here the 'profileinfo' key displays item count = 2.
But when it redirects to 'WebProfile' page i get the two key item name - 'name' and 'profileinfo'. the 'name' key contains it's value but the 'profileinfo' key displays item count = 0;
labshasan
Member
6 Points
4 Posts
Re: Session value lost when page redirects to another aspx page
Sep 08, 2011 11:50 AM|LINK
At last i have got one possible solution for my problem. What i have done is - i have taken the objects as static (1) and then comment out (2) the lines which was clearing the list items after inserting them into Session["PropertyInfo"].
1.
static List<HttpSessionStateSample.Entity.User> pProvider;
static HttpSessionStateSample.Entity.User userObj;
2.
pProvider.Clear();
pProvider = null;
I don't think this is the best or even the only solution. There may have some technical issues involved with such cases. Still looking for the logic behind it, because the why commenting these lines displaying correct list items (count=2) in both of the pages.
bleroy
All-Star
15455 Points
2301 Posts
AspNetTeam
Microsoft
Re: Session value lost when page redirects to another aspx page
Sep 14, 2011 08:29 PM|LINK
Ouch. Using static variables is a big no-no and a sure way to run into thread safety issues. Session is designed to deal with this. Working around a problem by re-implementing the standard component is a very bad idea. Jsut use the overload to the redirect API that doesn't terminate the thread.
----
This posting is provided "AS IS" with no warranties, and confers no rights.