[ Home | Syllabus | Course Notes | Assignments | Search]
|
Model |
Description |
|
In-proc |
Stores session state in-process to ASP.NET (that is, in Aspnet_wp.exe) |
|
State server |
Stores session state in an external “state server” process on the Web server or on a remote machine |
|
SQL Server |
Stores session state in a Microsoft SQL Server database on the Web server or on a remote machine |
In-Proc state goes away when the IIS on that server stops
State server - must start IIS correctly and add configuration information
<configuration> <system.web> <sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424" /> </system.web> </configuration>
And here’s one that places session state in a state server process on another machine identified by IP address:
<configuration> <system.web> <sessionState mode="StateServer" stateConnectionString="tcpip=192.168.1.2:42424" /> </system.web> </configuration>
///// Using a SQL database
<configuration> <system.web> <sessionState mode="SQLServer" sqlConnectionString="server=hawkeye;uid=sa;pwd=" /> </system.web> </configuration>
Need to be able to write object (as a string of bytes - called serializing) to the state store
[Serializable]
public class ShoppingCart {
You can change the session time-out period in three ways. Option number one is to edit Machine.config. Option number two is to place a statement like this one, which sets the session time-out to 60 minutes, in a local Web.config file:
<sessionState timeout="60" />
And option number three is to write a time-out value (in minutes) to the Timeout property of an HttpSessionState object:
Session.Timeout = 60;
An application can explicitly close a session by calling the session’s Abandon method:
Session.Abandon ();
NOTE: Due to difficulties in setting up database from home, I changed the data source to an XML file
Points to be demonstrated:
Shopping Cart kept in Session object
DataBound control tied to XML data source
Ability to add and remove from the Shopping Cart

First let's look at the global.asax file
<script language="C#" runat="server">
void Session_Start ()
{
Session["MyShoppingCart"] = new ShoppingCart
();
}
</script>
//// this page displays the available books for sale
<%@ Import Namespace="System.Data" %>
<html>
<body>
<h1>Congo.com</h1>
<form runat="server">
<table width="100%" bgcolor="teal">
<tr>
<td>
<asp:Button Text="View Cart" OnClick="OnViewCart"
RunAt="server" />
</td>
</tr>
</table>
<br>
<center>
<asp:DataGrid ID="MyDataGrid"
AutoGenerateColumns="false" CellPadding="2"
BorderWidth="1" BorderColor="lightgray"
Font-Name="Verdana" Font-Size="8pt"
GridLines="vertical" Width="90%"
OnItemCommand="OnItemCommand" RunAt="server">
<Columns>
<asp:BoundColumn HeaderText="Item ID"
DataField="title_id" />
<asp:BoundColumn HeaderText="Title"
DataField="title" />
<asp:BoundColumn HeaderText="Price"
DataField="price" DataFormatString="{0:c
}"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="right" />
<asp:ButtonColumn HeaderText="Action" Text="Add to Cart"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center"
CommandName="AddToCart" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white"
Font-Bold="true" />
<ItemStyle BackColor="white" ForeColor="darkblue" />
<AlternatingItemStyle BackColor="beige"
ForeColor="darkblue" />
</asp:DataGrid>
</center>
</form>
</body>
</html>
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
if (!IsPostBack) {
/*
string ConnectString =
ConfigurationSettings.AppSettings["connectString"];
SqlDataAdapter adapter = new SqlDataAdapter
("select * from titles where price != 0", ConnectString);
DataSet ds = new DataSet ();
adapter.Fill (ds);
MyDataGrid.DataSource = ds;
*/
DataSet ds = new DataSet ();
ds.ReadXml (Server.MapPath ("titles.xml"));
MyDataGrid.DataSource = ds;
MyDataGrid.DataBind ();
}
}
void OnItemCommand (Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "AddToCart") {
BookOrder order = new BookOrder (e.Item.Cells[0].Text
,
e.Item.Cells[1].Text, Convert.ToDecimal
(e.Item.Cells[2].Text.Substring (1)
), 1);
ShoppingCart cart = (ShoppingCart) Session["MyShoppingCart"];
if (cart != null)
cart.AddOrder (order);
}
}
void OnViewCart (Object sender, EventArgs e)
{
Response.Redirect ("ViewCart.aspx");
}
</script>
using System; using System.Collections; [Serializable]public class BookOrder { string _ItemID; string _Title; decimal _Price; int _Quantity; public string ItemID { get { return _ItemID; } set { _ItemID = value; } } public string Title { get { return _Title; } set { _Title = value; } } public decimal Price { get { return _Price; } set { _Price = value; } } public int Quantity { get { return _Quantity; } set { _Quantity = value; } } public BookOrder (string ItemID, string Title, decimal Price, int Quantity) { _ItemID = ItemID; _Title = Title; _Price = Price; _Quantity = Quantity; } } [Serializable] public class ShoppingCart { Hashtable _Orders = new Hashtable (); public ICollection Orders { get { return _Orders.Values; } } public decimal TotalCost { get { decimal total = 0; foreach (DictionaryEntry entry in _Orders) { BookOrder order = (BookOrder) entry.Value; total += (order.Price * order.Quantity); } return total; } } public void AddOrder (BookOrder Order) { BookOrder order = (BookOrder) _Orders[Order.ItemID];
if (order != null)// already have in the table order.Quantity += Order.Quantity; else _Orders.Add (Order.ItemID, Order);
} public void RemoveOrder (string ItemID) { if (_Orders[ItemID] != null) _Orders.Remove (ItemID); } }

<html>
<body>
<h1>Shopping Cart</h1>
<form runat="server">
<table width="100%" bgcolor="teal">
<tr>
<td>
<asp:Button Text="Return to Shopping" OnClick="OnShop"
RunAt="server" />
</td>
</tr>
</table>
<br>
<center>
<asp:DataGrid ID="MyDataGrid"
AutoGenerateColumns="false" CellPadding="2"
BorderWidth="1" BorderColor="lightgray"
Font-Name="Verdana" Font-Size="8pt"
GridLines="vertical" Width="90%"
OnItemCommand="OnItemCommand" RunAt="server">
<Columns>
<asp:BoundColumn HeaderText="Item ID"
DataField="ItemID" />
<asp:BoundColumn HeaderText="Title"
DataField="Title" />
<asp:BoundColumn HeaderText="Price"
DataField="Price" DataFormatString="{0:c}"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="right" />
<asp:BoundColumn HeaderText="Quantity"
DataField="Quantity"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center" />
<asp:ButtonColumn HeaderText="Action" Text="Remove"
HeaderStyle-HorizontalAlign="center"
ItemStyle-HorizontalAlign="center"
CommandName="RemoveFromCart" />
</Columns>
<HeaderStyle BackColor="teal" ForeColor="white"
Font-Bold="true" />
<ItemStyle BackColor="white" ForeColor="darkblue" />
<AlternatingItemStyle BackColor="beige"
ForeColor="darkblue" />
</asp:DataGrid>
</center>
<h3><asp:Label ID= "Total" RunAt="server" /></h3>
</form>
</body>
</html>
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
ShoppingCart cart = (ShoppingCart) Session["MyShoppingCart"];
if (cart != null) {
MyDataGrid.DataSource = cart.Orders;
MyDataGrid.DataBind ();
Total.Text = String.Format ("Total Cost: {0:c}",
cart.TotalCost);
}
}
void OnItemCommand (Object sender, DataGridCommandEventArgs e)
{
if (e.CommandName == "RemoveFromCart") {
ShoppingCart cart = (ShoppingCart) Session["MyShoppingCart"];
if (cart != null) {
cart.RemoveOrder (e.Item.Cells[0].Text);
MyDataGrid.DataBind ();
Total.Text = String.Format ("Total Cost: {0:c
}",
cart.TotalCost);
}
}
}
public void OnShop (Object sender, EventArgs e)
{
Response.Redirect ("Congo.aspx");
}
</script>
Here is the data source
<?xml version="1.0" encoding="UTF-8"?> <titles> <item> <title_id>BU1032</title_id> <title>The Busy Executive's Database Guide</title> <price>$20</price> </item> <item> <title_id>BU1111</title_id> <title>Cooking with Computers</title> <price>$50</price> </item> <item> <title_id>MC2234</title_id> <title>The Gourmet Microwave</title> <price>$30</price> </item> <item> <title_id>MX1230</title_id> <title>History of .NET</title> <price>$75.95</price> </item> </titles>
How to save this shopping cart?
F1: This class will be defined later - session remembers this one object
F2: Can access all items in the selected row as an array of cells
F3: skip over dollar sign
F4this will permit session state to be written to file
F5: See if this item already is in the HashTable
F6: Store Order under the ItemID as the Hash Key
F7: using an "ICollection" object as a data source, uses field Identifiers as properties?
F8: "c" is for currency format