Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Adding Item From Gridview To Shopping Cart

    I have modified my code where the data for the GridView is loaded when the page loads.

    Go to www.jeremyconrad2.net/pizza.aspx to view the page.

    As you can see the Totally Awesome Pizza page shows the pizza. These items are listed using a GridView and the data is loaded on page_load using the following code:

    Code:
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    public partial class pizza : System.Web.UI.Page
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet dtset = new DataSet();
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string DatabaseConnection = ConfigurationManager.ConnectionStrings["dbconnect"].ToString();
    
                SqlConnection connect = new SqlConnection(DatabaseConnection);
    
                connect.Open();
    
                SqlCommand myCommand = new SqlCommand("SELECT product.p_description, product.p_ingredient, inventory.inv_size, inventory.inv_price FROM inventory INNER JOIN product ON inventory.product_id = product.product_id WHERE (product.p_description = 'Totally Awesome Pizza')", connect);
    
                adapter.SelectCommand = myCommand;
                adapter.Fill(dtset, "ProductTable");
                GridView1.DataSource = dtset.Tables["ProductTable"];
                GridView1.DataBind();
                
                connect.Close();
            }
        }
    }
    I want to add an "Add" button but I also want the add button to add the pizza to the shopping cart.

    How do I use the add button to add the corresponding GridView Row to the shopping car?



    I have three classes; item.cs, ShoppingItem.cs, and ShoppingCart.cs.



    Here is the code for item.cs:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    /// <summary>
    /// Summary description for item
    /// </summary>
    public class item
    {
        public string Description { get; set; }
        public string Ingredients { get; set; }
        public string Size { get; set; }
        public decimal Price { get; set; }
        public string Prices { get; set; }
    
        public item(int ProductId)
        {
            string DatabaseConnection = ConfigurationManager.ConnectionStrings["dbconnect"].ToString();
    
            SqlConnection connect = new SqlConnection(DatabaseConnection);
    
            connect.Open();
    
            SqlCommand myCommand = new SqlCommand("SELECT product.p_description AS Description, inventory.inv_size AS Size, inventory.inv_price AS Price FROM inventory INNER JOIN product ON inventory.product_id = product.product_id WHERE (inventory.inv_id = @add_id)", connect);
            myCommand.Parameters.AddWithValue("@add_id", SqlDbType.Int).Value = ProductId;
    
            SqlDataReader reader = myCommand.ExecuteReader();
    
            reader.Read();
    
            this.Description = (string)reader["p_description"];
            this.Ingredients = (string)reader["p_ingredient"];
            this.Size = (string)reader["inv_size"];
            this.Price = (decimal)reader["inv_price"];
            this.Prices = Convert.ToString(Price);
    
            reader.Close();
            connect.Close();
        }
    }

    Here is the code for ShoppingItem.cs:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Configuration;
    using System.Data.SqlClient;
    using System.Data;
    
    /// <summary>
    /// Summary description for ShoppingCart
    /// </summary>
    public class ShoppingItem : IEquatable<ShoppingItem>
    {
    
        public int Quantity { get; set; }
    
        private int inventoryId;
        public int InventoryId
        {
            get { return inventoryId; }
            set { inventoryId = value; }
        }
    
        private item _item = null;
        public item Item
        {
            get
            {
                if (_item == null)
                {
                    _item = new item(InventoryId);
                }
                return _item;
            }
        }
    
        public string Description
        {
            get { return Item.Description; }
        }
    
        public string Ingredients
        {
            get {return Item.Ingredients;}
        }
    
        public string Size
        {
            get{ return Item.Size; }
        }
    
        public decimal Price
        {
            get { return Item.Price; }
        }
    
        public decimal TotalPrice
        {
            get { return Price * Quantity; }
        }
    
        public ShoppingItem(int ItemId)
        {
            this.InventoryId = ItemId;
        }
    
        public bool Equals(ShoppingItem item)
        {
            return item.InventoryId == this.InventoryId;
        }
    }

    Here is the code for ShoppingCart.cs:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ShoppingCart
    /// </summary>
    public class ShoppingCart
    {
        public List<ShoppingItem> Items { get; private set; }
        public static readonly ShoppingCart Instance;
    
        static ShoppingCart()
        {
            if (HttpContext.Current.Session["ShoppingCart"] == null)
            {
                Instance = new ShoppingCart();
                Instance.Items = new List<ShoppingItem>();
                HttpContext.Current.Session["ShoppingCart"] = Instance;
            }
            else
            {
                Instance = (ShoppingCart)HttpContext.Current.Session["ShoppingCart"];
            }
        }
    
        protected ShoppingCart() { }
    
        public void AddItem(int ItemsID)
        {
            ShoppingItem newItem = new ShoppingItem(ItemsID);
    
            if (Items.Contains(newItem))
            {
                foreach (ShoppingItem item in Items)
                {
                    if (item.Equals(newItem))
                    {
                        item.Quantity++;
                        return;
                    }
                }
            }
            else
            {
                newItem.Quantity = 1;
                Items.Add(newItem);
            }
        }
    
        public void SetItemQuantity(int ItemsID, int Quantity)
        {
            if (Quantity == 0)
            {
                RemoveItem(ItemsID);
                return;
            }
    
            ShoppingItem updatedItem = new ShoppingItem(ItemsID);
    
            foreach (ShoppingItem item in Items)
            {
                if (item.Equals(updatedItem))
                {
                    item.Quantity = Quantity;
                    return;
                }
            }
        }
    
        public void RemoveItem(int ItemsID)
        {
            ShoppingItem removedItem = new ShoppingItem(ItemsID);
            Items.Remove(removedItem);
        }
    
        public decimal GetSubTotal()
        {
            decimal subTotal = 0;
            foreach (ShoppingItem item in Items)
            {
                subTotal += item.TotalPrice;
            }
            return subTotal;
        }
    }
    I know I am close to getting this to work. I just need some advice on how to pass the selected row on the GridView from the pizza.aspx page to the shopping cart on the order.aspx page.

    Thank you in advance for your help.
    Last edited by germ1977; 03-24-2013 at 07:58 PM. Reason: Title Error


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •