Newbie Q: Using the session state object

  • Thread starter Thread starter Ant
  • Start date Start date
A

Ant

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top