JohnH
Posts: 43 Joined: 12/1/2004 Status: offline
|
Simple Shopping Cart - 4/16/2008 7:12:34
Hi all I found the following code on the net which is very close to my requirements. The amendments I would like to make are that I would like my products to be displayed in a drop down box and I would also like to add a quantity field (in the product.aspx page). Can anyone help? Source Code for Product.aspx
<%@ PAGE LANGUAGE="C#" AUTOEVENTWIREUP="true" CODEFILE="Product.aspx.cs" INHERITS="Product" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML XMLNS="http://www.w3.org/1999/xhtml">
<HEAD RUNAT="server">
<TITLE>My Shopping Cart</TITLE>
<style type="text/css">
.style2 {
font-family: Arial;
font-size: medium;
color: #8D1D1C;
}
.style3 {
font-family: Arial;
text-align: right;
}
.style4 {
font-family: Arial;
}
.style5 {
font-family: Arial;
font-size: small;
}
</style>
</HEAD>
<BODY>
<FORM ID="form1" RUNAT="server">
<table border="0" width="100%" id="table2" style="border-collapse: collapse" background="images/bground.jpg">
<tr>
<td class="style2">
<strong>Rail Requisition</strong></td>
<td class="style3">
<i><font face="Cooper Black" color="#ffffff" size="5">
HYDREX </font></i></td>
</tr>
</table>
<br>
<ASP:REPEATER ID="rptProducts" RUNAT="server">
<HEADERTEMPLATE>
<TABLE BORDER=1>
</HEADERTEMPLATE>
<ITEMTEMPLATE>
<TR>
<TD>
<span class="style5"><%#DataBinder.Eval(Container.DataItem,"ProductName") %></span>
</TD>
<td><asp:TextBox runat="server" id="TextBox1" Width="40px"></asp:TextBox></td>
<TD><a HREF='ShoppingCart.aspx?action=add&ID=<%#DataBinder.Eval(Container.DataItem,"ProductID")%>'><image border="0" src="images/add.jpg" width="12" height="12"></a></TD>
</TR>
</ITEMTEMPLATE>
</ASP:REPEATER>
<span class="style4">
<FOOTERTEMPLATE>
<span class="style5">
</span>
</FOOTERTEMPLATE>
<span class="style5">
<BR />
</span>
<A HREF="ShoppingCart.aspx"><span class="style5">My Shopping Cart</span></A></span>
<span class="style4"><span class="style5">
<BR />
</span>
<A HREF="Checkout.aspx"><span class="style5">CheckOut</span></A><span class="style5">
</span></span>
</FORM>
</BODY>
</HTML>
Product.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Product : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnString"].Trim());
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from products";
SqlDataReader dr = cmd.ExecuteReader();
rptProducts.DataSource = dr;
rptProducts.DataBind();
dr.Close();
conn.Close();
}
}
ShoppingCart.aspx
<%@ PAGE LANGUAGE="C#" AUTOEVENTWIREUP="true" CODEFILE="ShoppingCart.aspx.cs" INHERITS="ShoppingCart" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML XMLNS="http://www.w3.org/1999/xhtml">
<HEAD RUNAT="server">
<TITLE>Untitled Page</TITLE>
</HEAD>
<BODY>
<H2>
My Shopping Cart</H2>
<FORM ID="form1" RUNAT="server">
<BR />
<TABLE BORDER="1" WIDTH="80%" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD COLSPAN="2">
<ASP:LABEL ID="lblMsg" RUNAT="server"></ASP:LABEL>
</TD>
</TR>
<ASP:REPEATER ID="rptShoppingCart" RUNAT="server">
<HEADERTEMPLATE>
</HEADERTEMPLATE>
<ITEMTEMPLATE>
<TR>
<TD>
<%# DataBinder.Eval(Container.DataItem,"Counter") %>
.
<%# DataBinder.Eval(Container.DataItem,"ProductName") %>
</TD>
<TD>
<A HREF="ShoppingCart.aspx?action=remove&id=<%# DataBinder.Eval(Container.DataItem,"ProductID") %>">
Remove</A></TD>
</TR>
</ITEMTEMPLATE>
<FOOTERTEMPLATE>
</FOOTERTEMPLATE>
</ASP:REPEATER>
</TABLE>
<BR />
<A HREF='Product.aspx'>Continue Shopping</A>
<BR />
<A HREF=''>Check Out </A>
</FORM>
</BODY>
</HTML>
ShoppingCart.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] != null)
{
int iProductID = Convert.ToInt32(Request.QueryString["id"]);
if (Request.QueryString["action"].Trim().ToLower() == "add")
{
AddToShoppingCart(iProductID);
}
else
{
RemoveShoppingCart(iProductID);
}
}
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
if (Request.Cookies["ShoppingCart"] != null)
{
HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart"];
string sProductID = oCookie.Value.ToString();
if (sProductID.Length == 0)
{
lblMsg.Text = "<B>No items in your shopping cart<B><BR>";
}
else
{
char[] sep = { ',' };
string[] sArrProdID = sProductID.Split(sep);
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ProductID"));
dt.Columns.Add(new DataColumn("ProductName"));
dt.Columns.Add(new DataColumn("Counter"));
int counter = 1;
for (int i = 0; i < sArrProdID.Length; i++)
{
DataRow dr = dt.NewRow();
dr["ProductID"] = sArrProdID;
dr["ProductName"] = GetProductName(sArrProdID);
dr["Counter"] = counter;
dt.Rows.Add(dr);
counter++;
}
rptShoppingCart.DataSource = dt;
rptShoppingCart.DataBind();
}
}
}
private string GetProductName(string ProductID)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnString"].Trim());
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select ProductName from products where productid = '" + ProductID + "'";
string ProductName = cmd.ExecuteScalar().ToString();
conn.Close();
return ProductName;
}
private void AddToShoppingCart(int ProductID)
{
if (Request.Cookies["ShoppingCart"] == null)
{
HttpCookie oCookie = new HttpCookie("ShoppingCart");
//Set Cookie to expire in 3 hours
oCookie.Expires = DateTime.Now.AddHours(3);
oCookie.Value = ProductID.ToString();
Response.Cookies.Add(oCookie);
}
else
{
bool bExists = false;
char[] sep = { ',' };
HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart"];
//Set Cookie to expire in 3 hours
oCookie.Expires = DateTime.Now.AddHours(3);
//Check if Cookie already contain same item
string sProdID = oCookie.Value.ToString();
string[] arrCookie = sProdID.Split(sep);
for (int i = 0; i < arrCookie.Length; i++)
{
if (arrCookie.Trim() == ProductID.ToString().Trim())
{
bExists = true;
}
}
if (!bExists)
{
if (oCookie.Value.Length == 0)
{
oCookie.Value = ProductID.ToString();
}
else
{
oCookie.Value = oCookie.Value + "," + ProductID;
}
}
//Add back into the Response Objects.
Response.Cookies.Add(oCookie);
}
}
private void RemoveShoppingCart(int ProductID)
{
if (Request.Cookies["ShoppingCart"] == null)
{
//Do nothing
}
else
{
HttpCookie oCookie = (HttpCookie)Request.Cookies["ShoppingCart"];
//Set Cookie to expire in 3 hours
char[] sep = { ',' };
oCookie.Expires = DateTime.Now.AddHours(3);
//Check if Cookie already contain same item
string sProdID = oCookie.Value.ToString();
string[] arrCookie = sProdID.Split(sep);
string[] arrCookie2 = new string[arrCookie.Length - 1];
int j = 0;
for (int i = 0; i < arrCookie.Length; i++)
{
if (arrCookie.Trim() != ProductID.ToString())
{
arrCookie2[j] = arrCookie;
j++;
}
}
string sCookieID = "";
for (int i = 0; i < arrCookie2.Length; i++)
{
sCookieID = sCookieID + arrCookie2 + ",";
}
if (sCookieID.Length > 0)
{
oCookie.Value = sCookieID.Substring(0, sCookieID.Length - 1);
}
else
{
oCookie.Value = "";
}
//Add back into the Response Objects.
Response.Cookies.Add(oCookie);
}
}
}
Any help would be greatfully appreciated. Many thanks, John
|