SessionFarm from ASP.NET
SessionFarm allows you to access and update the old Session object’s variables from ASP.NET webforms. Here’s what you’ll need to do:
- Add a reference to SessionFarmNet.dll for your ASP.NET project. Installed into your sessionfarm install directory, usually:
c:\program files\groat.com\sessionfarm\sessionfarmnet.dll
- Load the ASPSession object. You’ll notice that when you create the ASPSession object you pass in a connectionstring to the database, and the current webform’s Request and Response objects.
Groat.SessionFarm.ASPSession aspsession = new Groat.SessionFarm.ASPSession(ConnectionString, Request, Response);
- There are several ways to read a Session variable:
aspsession.GetValue(“variablename”); (will return “” if variable not found) aspsession.Contents[“variablename”]; (will throw an exception if variable doesn’t exist)
- You can use foreach to loop through all of the Session variables:
foreach(Object obj in aspsession) { DictionaryEntry entry = (DictionaryEntry)obj; Response.Write(entry.Key); Response.Write(" = "); Response.Write(entry.Value); }
- There are several ways to set a Session variable (be sure to see point #6 on saving changes):
aspsession.SetValue(“variablename”, “new value”); (works if variable exists or not) aspsession.Contents[“variablename”] = “new value”; (if the variable exists in contents)
- If you want to save your changes to the Session variables:
aspsession.Save();