Example #1
0
        public JsonResult UpdateCartInfo(Ordered_Details order_details)
        {
            bool add_product = false;

            Ordered_Details temp_data = new Ordered_Details()
            {
                ProductId        = order_details.ProductId,
                Ordered_Quantity = order_details.Ordered_Quantity
            };
            List <string> old_history_lis = (List <string>)Session["history"];

            // Ordered Product ID and Quantity
            var query = from x in old_history_lis
                        group x by x into g
                        let count = g.Count()
                                    orderby count descending
                                    select new { Value = g.Key, Count = count };

            foreach (var old_history_data in query)
            {
                if (old_history_data.Value == temp_data.ProductId)
                {
                    if (old_history_data.Count < temp_data.Ordered_Quantity)
                    {
                        add_product = true;
                    }
                }
            }

            int temp_update_productID = Int32.Parse(temp_data.ProductId);

            if (add_product)
            {
                old_history_lis.Add(temp_update_productID.ToString());
            }
            else
            {
                old_history_lis.Remove(temp_update_productID.ToString());
            }

            // Update session
            Session["history"] = null;
            Session["history"] = old_history_lis;

            // Calculate Total Cost
            List <Ordered_Details> temp_orderedDetails = Add_ProductID_Qua();
            int    temp_totalPrice = total_price;
            object new_total_price = new
            {
                total_price = temp_totalPrice
            };

            return(Json(new_total_price, JsonRequestBehavior.AllowGet));
        }
Example #2
0
        public List <Ordered_Details> Add_ProductID_Qua()
        {
            total_price = 0;

            List <string> order_product_lis = (List <string>)Session["history"];

            if (order_product_lis.Count == 0)
            {
                return(null);
            }

            List <Ordered_Details> ordered_products = new List <Ordered_Details>();

            // Ordered Product ID and Quantity
            var query = from x in order_product_lis
                        group x by x into g
                        let count = g.Count()
                                    orderby count descending
                                    select new { Value = g.Key, Count = count };


            foreach (var q in query)
            {
                using (SqlConnection sql_connection = new SqlConnection(Data.Data.conn))
                {
                    sql_connection.Open();

                    string sql_data = "select * from Product where Id=" + q.Value + ";";

                    SqlCommand sql_cmd = new SqlCommand(sql_data, sql_connection);

                    SqlDataReader reader = sql_cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        Product pro = new Product()
                        {
                            Id       = (int)reader["Id"],
                            name     = (string)reader["name"],
                            price    = (string)reader["price"],
                            quantity = (string)reader["quantity"],

                            short_description = (string)reader["short_description"],
                            image_path        = (string)reader["image_path"],
                        };

                        Ordered_Details temp_data = new Ordered_Details()
                        {
                            CustomerId         = (string)Session["Correct_User"],
                            ProductId          = q.Value.ToString(),
                            Ordered_Quantity   = q.Count,
                            Ordered_Date       = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day),
                            Product_Details    = pro.short_description,
                            Product_Image_Path = pro.image_path,
                            Product_Name       = pro.name,
                            Product_Price      = Convert.ToInt32(pro.price.Replace("$", String.Empty))
                        };
                        ordered_products.Add(temp_data);
                    }
                    sql_connection.Close();
                }
            }
            foreach (Ordered_Details temp_data in ordered_products)
            {
                total_price = total_price + (temp_data.Ordered_Quantity * temp_data.Product_Price);
            }
            return(ordered_products);
        }