private void AddCart() { CartTableEntity ce = new CartTableEntity(); try { var lst = abal.GetProducts(); Console.WriteLine("{0,10}{1,20}{2,20}{3,20}", "ID", "Product Name", "Price", "Discount"); foreach (var l in lst) { Console.WriteLine("{0,10}{1,20}{2,20}{3,20}", l.ProdId, l.ProductName, l.Price, l.Discount); } Console.WriteLine("Enter ProductID"); ce.ProdID = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Quantity"); ce.Quantity = int.Parse(Console.ReadLine()); var res = ubal.AddCart(ce); if (res) { Console.WriteLine("Product Added to Cart Successfully"); } Console.WriteLine("Do You Want to Continue(Y/N)?"); char ans = char.Parse(Console.ReadLine()); if ((ans == 'Y') || (ans == 'y')) { MenuUser(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public bool AddCart(CartTableEntity cte) { try { if (cte.ProdID <= 0) { throw new Exception("Product ID cannot be zero or negative"); } if (cte.Quantity <= 0) { throw new Exception("Cannot order zero or negative quantity"); } return(dal.AddCart(cte)); } catch (ApplicationException ex) { throw ex; } }
public bool AddCart(CartTableEntity cte) { bool result = false; decimal price = 0.0M; decimal discount = 0.0M; try { cte.UserId = LoginDAL.UID; if (con.State != ConnectionState.Open) { con.Open(); } string q = "select price,discount from ProductTable where prodid=@pid"; string query = "insert into ShoppingCartTable values(@uid,@pid,@qty,@odate,@totamt)"; com1 = con.CreateCommand(); com1.CommandText = q; IDataParameter pid = com1.CreateParameter(); pid.ParameterName = "@pid"; pid.Value = cte.ProdID; com1.Parameters.Add(pid); r = com1.ExecuteReader(); if (r.Read()) { price = Convert.ToDecimal(r[0]); discount = Convert.ToDecimal(r[1]); } r.Close(); com = con.CreateCommand(); com.CommandText = query; IDataParameter uid = com.CreateParameter(); uid.ParameterName = "@uid"; uid.Value = cte.UserId; com.Parameters.Add(uid); IDataParameter prid = com.CreateParameter(); prid.ParameterName = "@pid"; prid.Value = cte.ProdID; com.Parameters.Add(prid); IDataParameter qty = com.CreateParameter(); qty.ParameterName = "@qty"; qty.Value = cte.Quantity; com.Parameters.Add(qty); cte.OrderDate = DateTime.Now; IDataParameter odate = com.CreateParameter(); odate.ParameterName = "@odate"; odate.Value = cte.OrderDate; com.Parameters.Add(odate); cte.TotalAmount = ((Convert.ToDecimal(cte.Quantity) * price) - (discount * cte.Quantity)); IDataParameter tot = com.CreateParameter(); tot.ParameterName = "@totamt"; tot.Value = cte.TotalAmount; com.Parameters.Add(tot); var res = com.ExecuteNonQuery(); if (res > 0) { result = true; } } catch (Exception ex) { throw ex; } finally { con.Close(); } return(result); }