Session.State and passing variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    #1

    Session.State and passing variables

    I'm trying to pass some variables from one page to another via the session state and then post those variables to labels on page 2. When I try to write the code for my labels on page2, I get an error of "Declaratio n expected"

    Here is my code from that page:

    Code:
    Partial Class Default2
        Inherits System.Web.UI.Page
        Dim firstname As String = CType(Session.Item("FirstName"), String)
        Dim lastName As String = CType(Session.Item("LastName"), String)
        Dim DateOfBirth As String = CType(Session.Item("DateOfBirth"), String)
        Dim Age As String = CType(Session.Item("Age"), String)
    Am I missing a page_load event handler or what am I missing?

    Thank you,

    Doug
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Try this:
    Code:
        Dim firstname As String = CType(Session("FirstName"), String)
        Dim lastName As String = CType(Session("LastName"), String)
        Dim DateOfBirth As String = CType(Session("DateOfBirth"), String)
        Dim Age As String = CType(Session("Age"), String)
    If that doesn't work (and I don't think it will) try this:
    Code:
    Partial Class Default2
        Inherits System.Web.UI.Page
    
        Private firstname As String
        Private lastName As String 
        Private DateOfBirth As String 
        Private Age As String 
        
         Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
               firstname = CType(Session("FirstName"), String)
               lastName = CType(Session("LastName"), String)
               DateOfBirth = CType(Session("DateOfBirth"), String)
               Age = CType(Session("Age"), String)
         End Sub
         '.....
    End Class
    Also, make sure that the "key" that you use to set the variable in Session matches the key that you use to retrieve the item from session.

    -Frinny
    Last edited by Frinavale; Oct 15 '10, 06:24 PM.

    Comment

    • dougancil
      Contributor
      • Apr 2010
      • 347

      #3
      Frinny,

      I discovered that I was missing a page_load event in the second page. After I added that, it worked as it should have.

      Comment

      Working...