2012-09-03 78 views
2

我創建了這個函數,但它似乎給我一個問題。我想將字典存儲到Session變量中,以便我可以訪問整個網站中的字典。我不斷收到錯誤所需對象:DictionaryObject或它會說此密鑰已存在於字典。有人可以告訴我我做錯了什麼嗎?將字典存儲到會話中

我在本文中看過storing dictionary in session,但並沒有真正適合我所要做的!

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 

回答

2

我能夠得到它的工作,這是我做了什麼?如果有人看到任何錯誤,或者如果我需要添加任何錯誤陷印。這是一次加載時

昏暗的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 

您可以像這樣訪問:

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