Session State
public class Furniture
public string Name;
public string Description;
public decimal Cost;
public Furniture(string name, string description,
decimal cost)
{
Name = name;
Description = description;
Cost = cost;
}
}
{
protected void Page_Load(Object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// Create Furniture objects.
Furniture piece1 = new Furniture("Econo Sofa",
"Acme Inc.", 74.99M);
Furniture piece2 = new Furniture("Pioneer Table",
"Heritage Unit", 866.75M);
Furniture piece3 = new Furniture("Retro Cabinet",
"Sixties Ltd.", 300.11M);
// Add objects to session state.
Session["Furniture1"] = piece1;
Session["Furniture2"] = piece2;
Session["Furniture3"] = piece3;
// Add rows to list control.
lstItems.Items.Add(piece1.Name);
lstItems.Items.Add(piece2.Name);
lstItems.Items.Add(piece3.Name);
}
// Display some basic information about the session.
// This is useful for testing configuration settings.
lblSession.Text = "Session ID: " + Session.SessionID;
lblSession.Text += "<br />Number of Objects: ";
lblSession.Text += Session.Count.ToString();
lblSession.Text += "<br />Mode: " + Session.Mode.ToString();
lblSession.Text += "<br />Is Cookieless: ";
lblSession.Text += Session.IsCookieless.ToString();
lblSession.Text += "<br />Is New: ";
lblSession.Text += Session.IsNewSession.ToString();
lblSession.Text += "<br />Timeout (minutes): ";
lblSession.Text += Session.Timeout.ToString();
}
protected void cmdMoreInfo_Click(Object sender, EventArgs e)
{
if (lstItems.SelectedIndex == -1)
{
lblRecord.Text = "No item selected.";
}
else
{
// Construct the right key name based on the index.
string key = "Furniture" +
(lstItems.SelectedIndex + 1).ToString();
// Retrieve the Furniture object from session state.
Furniture piece = (Furniture)Session[key];
// Display the information for this object.
lblRecord.Text = "Name: " + piece.Name;
lblRecord.Text += "<br />Manufacturer: ";
lblRecord.Text += piece.Description;
lblRecord.Text += "<br />Cost: " + piece.Cost.ToString("c");
}
}
}
Session State Configuration
The following listing shows the most important op tions that you can set for the <sessionState>
<?xml version="1.0" ?>
<system.web>
<!-- Other settings omitted. -->
<sessionState
cookieless="UseCookies"
cookieName="ASP.NET_SessionId"
regenerateExpiredSessionId="false"
timeout="20"
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
stateNetworkTimeout="10"
sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
sqlCommandTimeout="30"
allowCustomSqlDatabase="false"
customProvider=""
compressionEnabled="false"
/>
</system.web>
</configuration>
Mode
InProc is the default mode, and it makes the most sense for small websites.
StateServer
Here’s a command that creates the session storage database on the current computer, using the default database name ASPState:
sqlConnectionString="data source=127.0.0.1;Integrated Security=SSPI"
... />
Custom
Compression
Application State
{
// Retrieve the current counter value.
int count = 0;
if (Application["HitCounterForOrderPage"] != null)
{
count = (int)Application["HitCounterForOrderPage"];
}
// Increment the counter.
count++;
// Store the current counter value.
Application["HitCounterForOrderPage"] = count;
lblCounter.Text = count.ToString();
}
Application state isn’t often used, because it’s gen erally inefficient. In the previous example, the counter would probably not keep an accurate count, partic ularly in times of heavy traffic.
To prevent this problem, you need to use the Lo ck() and Unlock() methods, which explicitly allow only one client to access the Application state collection at a time.
To prevent this problem, you need to use the Lo ck() and Unlock() methods, which explicitly allow only
protected void Page_Load(Object sender, EventArgs e)
{
// Acquire exclusive access.
Application.Lock();
int count = 0;
if (Application["HitCounterForOrderPage"] != null)
{
count = (int)Application["HitCounterForOrderPage"];
}
count++;
Application["HitCounterForOrderPage"]=count;
//Releaseexclusiveaccess.
Application.Unlock();
lblCounter.Text=count.ToString();
}