http://www.dotnetbips.com/displayarticle.aspx?id=286
begin:
Download Source CodeDeveloping a Shopping Cart - Part 3
Introduction
In the previous article we saw how to create a shopping cart using session variables. Continuing the concept further this article will illustrate how to use store your shopping cart in a database. This technique is more robust and scalable that the previous two techniques.
Creating a database table
Before you proceed with any coding, you need to create the following table in the Northwind database of SQL Server.
CREATE TABLE [dbo].[ShoppingCart_Products] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[CartID] [varchar] (50),
[ProductID] [int] NULL,
[ProductName] [varchar] (255),
[UnitPrice] [money] NULL ,
[Quantity] [int] NULL
) ON [PRIMARY]
Developing a simple product listing page
We will first build a simple web form that lists Products table of Northwind database in a DataGrid.
- Create a new web project in VS.NET with C# as the language.
- Add a web form called ProductCatalog.aspx to it
- Drag and drop a DataGrid control on it.
- Write a function called BindGrid() as shown below:
private void BindGrid()
{
SqlDataAdapter da=
new SqlDataAdapter
("select * from products",
@"data source=.\vsdotnet;initial catalog=northwind;user id=sa");
DataSet ds=new DataSet();
da.Fill(ds,"products");
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
- Call this function in the Page_Load event handler
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindGrid();
}
}
- Write following code in the SelectedIndexChanged event of the DataGrid.
private void DataGrid1_SelectedIndexChanged
(object sender, System.EventArgs e)
{
CShoppingCartItem item=new CShoppingCartItem();
item.ProductID=
int.Parse(DataGrid1.SelectedItem.Cells[1].Text);
item.ProductName=
DataGrid1.SelectedItem.Cells[2].Text;
item.UnitPrice=
decimal.Parse(DataGrid1.SelectedItem.Cells[3].Text);
item.Quantity=1;
CShoppingCart.AddItem(Session.SessionID,item);
}
Here, create an instance of CSHoppingCartItem class and set its properties. We then call AddMethod of CShoppingCart class. Both of these classes are explained in the following text.
- Drag and drop a button control on the web form and write following code in the it's click event handler.
private void Button1_Click
(object sender, System.EventArgs e)
{
Response.Redirect("cart.aspx");
}
Here, we are simply navigating to the cart.aspx page which displays the shopping cart.
The CShoppingCartItem class
This class represents a single item of the shopping cart and looks as shown below:
public class CShoppingCartItem
{
private int intProductID;
private string strProductName;
private decimal decUnitPrice;
private int intQuantity;
public int ProductID
{
get
{
return intProductID;
}
set
{
intProductID=value;
}
}
public string ProductName
{
get
{
return strProductName;
}
set
{
strProductName=value;
}
}
public decimal UnitPrice
{
get
{
return decUnitPrice;
}
set
{
decUnitPrice=value;
}
}
public int Quantity
{
get
{
return intQuantity;
}
set
{
intQuantity=value;
}
}
}
The CShoppingCart class
This is the most important class in our application because it actually performs the job of storing or retrieving shopping cart items into a database table. It consists of static methods and looks as shown below:
public class CShoppingCart
{
private static string connstr=
@"data source=.\vsdotnet;initial
catalog=northwind;user id=sa";
public static
void AddItem(string cartid,CShoppingCartItem item)
{
SqlConnection cnn=new SqlConnection(connstr);
SqlCommand cmd=new SqlCommand();
cmd.Connection=cnn;
cmd.CommandText=
"insert into ShoppingCart_Products(cartid,productid,
productname,unitprice,quantity)
values(@cartid,@prodid,@prodname,@unitprice,@qty)";
SqlParameter p1=new SqlParameter("@cartid",cartid);
SqlParameter p2=new
SqlParameter("@prodid",item.ProductID);
SqlParameter p3=new
SqlParameter("@prodname",item.ProductName);
SqlParameter p4=new
SqlParameter("@unitprice",item.UnitPrice);文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



