Frank G.
Frank G.

Reputation: 1579

Storing a Dictionary to a Session

I created this function but it seems to give me a problem. I want to store a dictionary into an Session variable so I can access the dictionary throughout the website. I keep getting the error Object required: DictionaryObject or it will say This key already exist in the dictionary. Can someone please tell me what I am doing wrong?

I did look storing dictionary in session at this posting but didn't really fit what I am trying to do!

Function LoadPermissions()

    Dim SQLString
    SQLString ="SELECT datafields here... FROM " & TBL_employees_permissions & " AS p WHERE p.eid = '" & Clng(12) & "';"

    If IsObject(Session("dicPermissions")) = True Then
        Set dicPermissions = Session("dicPermissions")
    Else
        Set dicPermissions = Server.CreateObject("Scripting.Dictionary")
    End If

    db_conn conn, rs '
    Set myRS = conn.Execute (SQLString) 

    For each item in myRS.Fields
        If IsObject(Session("dicPermissions")) = True AND DictionaryObject.Exists(Trim(item.Name)) = False Then
            dicPermissions.Add Trim(item.Name), Trim(myRS(item.Name))
        End If
    Next
    db_disconn conn, rs

    Set Session("dicPermissions") = dicPermissions 'Store Dictionary to session array.

End Function

Upvotes: 2

Views: 2838

Answers (1)

Frank G.
Frank G.

Reputation: 1579

I was able to get it working and here is what I did? If anyone see anything wrong or if I need to add in any error trapping. This is load once when the user logs in.

Dim SQLString

    SQLString ="SELECT Datefields here... & " AS p WHERE p.eid = '" & Clng(12) & "';"

    'Create the dictionary object.
    Set Session("dicPermissions") = Server.CreateObject("Scripting.Dictionary") 'Create the Dictionary object.

    'sets up a connection to the database
    db_conn conn, rs 'Open account table.
    Set myRS = conn.Execute (SQLString) ' Uses any ADODB connection

    For each item in myRS.Fields 'Create the dictionary with the field names and cell data.
            'dicPermissions.Add fieldname, feild value
            Session("dicPermissions").Add Trim(item.Name), Trim(myRS(item.Name))
    Next
    db_disconn conn, rs 'Close the database

You can access it like so:

Response.write Session("dicPermissions").Item("itemnamehere...")

Upvotes: 2

Related Questions