Newbie Q: Using the session state object

Discussion in 'ASP .Net' started by Ant, Dec 20, 2007.

  1. Ant

    Ant Guest

    Hi,

    I'm experimenting with the session state to see how it works. I'm trying to
    increment with every page load. When I try to do this, I get an error
    indicating that the page state object is either null or not instantiated. I
    can't seem to get around this.
    Below is the code I'm using:

    Page.Session["MyInc"] = Int32.Parse((Page.Session["MyInc"].ToString()))+1;
    labelOP.Text = Page.Session["MyInc"].ToString();

    It's ok when I build, just when I run do I get the error.

    Why does this not work?

    Many thanks for helping me understand how to use this correctly
    Ant
     
    Ant, Dec 20, 2007
    #1
    1. Advertising

  2. Ant

    Manish Guest

    You would need to assign the value to Session variable in the Session_start
    event in the Global.asax like below:

    Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when a new session is started
    Session("test") = 1234
    End Sub

    Then you can increment the value of the Session in the Page Load event
    whenever it postbacks.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim intValue As Integer
    intValue = CInt(Session("test"))
    intValue += 1
    Session("test") = intValue.ToString()
    Response.Write(Session("test"))
    End Sub

    Regards,
    Manish
    www.componentone.com

    "Ant" wrote:

    > Hi,
    >
    > I'm experimenting with the session state to see how it works. I'm trying to
    > increment with every page load. When I try to do this, I get an error
    > indicating that the page state object is either null or not instantiated. I
    > can't seem to get around this.
    > Below is the code I'm using:
    >
    > Page.Session["MyInc"] = Int32.Parse((Page.Session["MyInc"].ToString()))+1;
    > labelOP.Text = Page.Session["MyInc"].ToString();
    >
    > It's ok when I build, just when I run do I get the error.
    >
    > Why does this not work?
    >
    > Many thanks for helping me understand how to use this correctly
    > Ant
     
    Manish, Dec 20, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shamanthakamani
    Replies:
    1
    Views:
    3,548
    Natty Gur
    Nov 20, 2003
  2. Not Liking Dot Net Today
    Replies:
    0
    Views:
    672
    Not Liking Dot Net Today
    Apr 21, 2004
  3. Maciek
    Replies:
    0
    Views:
    8,303
    Maciek
    Sep 15, 2005
  4. jnickfl1
    Replies:
    0
    Views:
    631
    jnickfl1
    Sep 18, 2006
  5. Harry Haller
    Replies:
    0
    Views:
    1,686
    Harry Haller
    Nov 7, 2006
Loading...

Share This Page