Shared Session Data

Discussion in 'ASP .Net' started by MS ASP.NET, May 19, 2005.

  1. MS ASP.NET

    MS ASP.NET Guest

    I'm using sql server session state and wrote a class to wrap current
    session. I need to know if what I am doing will result in users
    sharing session data.

    '----------------------------------
    '------- Session Wrapper --------
    '-----------------------------------
    Imports System.Web.SessionState
    Public Class EmployeeSession

    Private _session As HttpSessionState

    Sub New()
    _session = HttpContext.Current.Session
    End Sub

    Property EmployeeID() As Integer
    Get
    Return _session("EmployeeID")
    End Get
    Set(ByVal Value As Integer)
    _session("EmployeeID") = Value
    End Set
    End Property

    End Class
    '-----------------------------------


    '-----------------------------------
    '-------- Sub on a web form -------
    '-----------------------------------

    Private Sub DoSomething
    Dim empSession as new EmployeeSession

    empSession.EmployeeID=someValue
    End Sub

    '----------------------------------


    Any ideas or help appreciated

    Steve
    MS ASP.NET, May 19, 2005
    #1
    1. Advertising

  2. MS ASP.NET

    Marina Guest

    No, it will not.
    In fact, I would make all the methods in EmployeeSession Shared. So you
    don't need to bother instantiating it every time. Then something like this:

    Public Shared Property EmployeeID() As Integer
    Get
    CInt(GetItem("EmployeeID"))
    End Get
    Set(Value As Integer)
    SetItem("EmployeeID",Value)
    End Set
    End Property

    Private Shared Function GetItem(itemName As String) As Object
    Return HttpContext.Current.Session(itemName)
    End Function

    Private Shared Sub SetItem(itemName As String, itemValue As String)
    HttpContext.Current.Session(itemName) = itemValue
    End Sub

    That way you can say EmployeeSession.EmployeeID, without instantiating
    anything. Also, all the functionality of how everything is stored, is
    actually in GetItem and SetItem, properties call out to that.

    And lastly, please turn Option Strict On in your project, and make this the
    default. This will save you many hours by catching errors at run time.

    "MS ASP.NET" <> wrote in message
    news:...
    > I'm using sql server session state and wrote a class to wrap current
    > session. I need to know if what I am doing will result in users
    > sharing session data.
    >
    > '----------------------------------
    > '------- Session Wrapper --------
    > '-----------------------------------
    > Imports System.Web.SessionState
    > Public Class EmployeeSession
    >
    > Private _session As HttpSessionState
    >
    > Sub New()
    > _session = HttpContext.Current.Session
    > End Sub
    >
    > Property EmployeeID() As Integer
    > Get
    > Return _session("EmployeeID")
    > End Get
    > Set(ByVal Value As Integer)
    > _session("EmployeeID") = Value
    > End Set
    > End Property
    >
    > End Class
    > '-----------------------------------
    >
    >
    > '-----------------------------------
    > '-------- Sub on a web form -------
    > '-----------------------------------
    >
    > Private Sub DoSomething
    > Dim empSession as new EmployeeSession
    >
    > empSession.EmployeeID=someValue
    > End Sub
    >
    > '----------------------------------
    >
    >
    > Any ideas or help appreciated
    >
    > Steve
    >
    Marina, May 19, 2005
    #2
    1. Advertising

  3. MS ASP.NET

    MS ASP.NET Guest

    Beautiful!

    Thanks for your help!
    MS ASP.NET, May 19, 2005
    #3
  4. MS ASP.NET

    Marina Guest

    No Problem.
    Second parameter to SetItem should be an Object, so you can put anything in
    there. It was just to give the basic ide anyway...

    "MS ASP.NET" <> wrote in message
    news:...
    > Beautiful!
    >
    > Thanks for your help!
    >
    Marina, May 19, 2005
    #4
    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. DJ Dev
    Replies:
    3
    Views:
    16,098
    Gandalf
    Feb 8, 2004
  2. Joe Fallon
    Replies:
    3
    Views:
    753
    =?Utf-8?B?Z3V5?=
    Jul 16, 2004
  3. ben
    Replies:
    3
    Views:
    504
    Kevin Spencer
    Nov 15, 2004
  4. tshad
    Replies:
    11
    Views:
    808
    tshad
    May 27, 2005
  5. Imran Aziz
    Replies:
    1
    Views:
    531
    Stefan
    Aug 22, 2005
Loading...

Share This Page