session problem

Last post 01-24-2006 3:42 AM by crystal.net. 5 replies.

Sort Posts:

  • session problem

    01-23-2006, 3:47 AM
    • Member
      449 point Member
    • RioDD
    • Member since 05-18-2004, 10:08 AM
    • Macedonia & Mauritius
    • Posts 268

    Hi,

    I have problem with variables I put in session. I have a page where I put values into the session into a different class. On the next page those values are null. Can somebody help me?

    Thanks

    Dont forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped
  • Re: session problem

    01-23-2006, 6:58 AM
    • Member
      100 point Member
    • crystal.net
    • Member since 01-19-2006, 8:11 AM
    • Posts 20
    maybe you make an error by casting the values. can you please post your app's code, so we can better understand your problem. thanks.
  • Re: session problem

    01-23-2006, 8:33 AM
    • Member
      449 point Member
    • RioDD
    • Member since 05-18-2004, 10:08 AM
    • Macedonia & Mauritius
    • Posts 268

    I dont think that casting is a problem here.

    I use UserSession class for for storing variables:

    public class UserSession:System.Web.UI.Page

    {

    public UserSession()

    {

    }

    public static Int32 Priv1

    {

    get

    {

    UserSession vars = new UserSession();

    if(vars.Session["Priv1"].Equals((Int32)0)) return 0;

    return (Int32)(vars.Session["Priv1"]);

    }

    set

    {

    UserSession vars = new UserSession();

    vars.Session["Priv1"] = value;

    }

    }

    public static Int32 Priv2

    {

    get

    {

    UserSession vars = new UserSession();

    if(vars.Session["Priv2"].Equals((Int32)0)) return 0;

    return (Int32)(vars.Session["Priv2"]);

    }

    set

    {

    UserSession vars = new UserSession();

    vars.Session["Priv2"] = value;

    }

    }

    }

    Then I call this class:

    UserSession.Priv1=1;

    UserSession.Priv2=1;

    and then I redirect it to another page.

    When I debug, on the first page UserSession.Priv1 and UserSession.Priv2 are filled with values, but when I make Response.Redirect their value is lost. 

    Dont forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped
  • Re: session problem

    01-23-2006, 9:36 AM
    • Member
      100 point Member
    • crystal.net
    • Member since 01-19-2006, 8:11 AM
    • Posts 20

    okay, i've tested your code. i haven't lost any session values!
    i made an UserSession.cs file with your posted code in it and added this item to the App_Code "system folder". after initializing Priv1 & Priv2 class fields i've done a postback with a button which redirects to another *.aspx page. no probs so far! that's why i think you have configuration problems. maybe your browser doesn't support cookies or most likely the cookies are blocked? i would suggest that you test your code with following setup in web.config:

    <system.web>
       
    <sessionState cookieless="true" />
       ...
    </system.web>

    now, session ID's are stored in URL. does it work now?

  • Re: session problem

    01-23-2006, 10:07 AM
    • Member
      449 point Member
    • RioDD
    • Member since 05-18-2004, 10:08 AM
    • Macedonia & Mauritius
    • Posts 268

    I've channged the 

    <system.web>
       
    <sessionState cookieless="true" />
       ...
    </system.web>
     and I still have the same problem. Here is the error it returns on Response.Redirect

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 108:			{
    Line 109:				UserSession vars = new UserSession();
    Line 110:				if(vars.Session["Priv2"].Equals((Int32)0)) return 0;
    Line 111:				return (Int32)(vars.Session["Priv2"]);
    Line 112:			}

    Dont forget to click "Mark as Answer" on the post that helped you. This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped
  • Re: session problem

    01-24-2006, 3:42 AM
    • Member
      100 point Member
    • crystal.net
    • Member since 01-19-2006, 8:11 AM
    • Posts 20
    okay, the facts are clearer after your last post. you wrote first "On the next page those values are null" - but the main point is that not the values are null, it is the object itselfs. that's a difference and points again to my suggestion that you have a general problem with your session configuration.

    have you ever tried with a very easy example to handle sessions with a redirect:

    Testing.aspx.cs

    public partial class Testing : System.Web.UI.Page
    {
       
    protected void Page_Load(object sender, EventArgs e)
       {
          
    Session.Add("Priv1", 315);
          Response.Redirect(
    "~/Testing2.aspx");
       }
    }


    Testing2.aspx.cs

    public partial class Testing2 : System.Web.UI.Page
    {
       
    protected void Page_Load(object sender, EventArgs e)
       
    {
          
    Response.Write(Session["Priv1"].ToString());
       }
    }


    if this example works, session state configuration is NOT the problem. so, you may have put your code file (UserSession class) in wrong place. ensure that you add this class item to the App_Code directory, else you have to include it with its namespace.

Page 1 of 1 (6 items)