Can

  • 13 years ago

     

  • 13 years ago

     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 

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon