This may not be 'exact' but it is probably 99%:
<script runat="server">
System.Data.DataTable objDT;
System.Data.DataRow objDR;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
makeCart();
}
}
void makeCart()
{
objDT = new System.Data.DataTable("Cart");
objDT.Columns.Add("ID", GetType(Integer));
objDT.Columns("ID").AutoIncrement = True;
objDT.Columns("ID").AutoIncrementSeed = 1;
objDT.Columns.Add("Quantity", GetType(Integer));
objDT.Columns.Add("Product", GetType(String))objDT.Columns.Add("Cost", GetType(Decimal));
Session["Cart"] = objDT;
}
void AddToCart(object s, EventArgs e)
{
objDT = (System.Data.DataTabl) Session["Cart"];
string Product = ddlProducts.SelectedItem.Text;
bool blnMatch = false;
foreach( objDR in objDT.Rows)
{
if (objDR["Product"] = Product)
{
objDR["Quantity"] += txtQuantity.Text;
blnMatch = true;
break;
}
}
if (!blnMatch)
{
objDR = objDT.NewRow();
objDR["Quantity"] = txtQuantity.Text;
objDR["Product"] = ddlProducts.SelectedItem.Text;
objDR["Cost"] = Decimal.Parse(ddlProducts.SelectedItem.Value);
objDT.Rows.Add(objDR);
}
Session["Cart"] = objDT;
dg.DataSource = objDT;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
decimal GetItemTotal()
{
decimal decRunningTotal;
for (int intCounter = 0; intCounter = objDT.Rows.Count - 1; intCounter++)
{
objDR = objDT.Rows(intCounter);
decRunningTotal += (objDR["Cost"] * objDR["Quantity"]);
}
return decRunningTotal;
}
void DeleteItems(object s, DataGridCommandEventArgs e)
{
objDT = Session["Cart"];
objDT.Rows(e.Item.ItemIndex).Delete();
Session["Cart"] = objDT;
dg.DataSource = objDT;
dg.DataBind();
lblTotal.Text = "$" + GetItemTotal();
}
</script>
Joe
Enter your message below
Sign in or Join us (it's free).