public void AddToCart(int id) { // Retrieve the product from the database. string ShoppingCartId = GetCatrId(); var user = from u in DB.users where u.userName == ShoppingCartId select u; WebDL.user userdata = user.FirstOrDefault(); var cartItem = DB.shoppingCarts.FirstOrDefault( c => c.user.userName == ShoppingCartId && c.productID == id); if (cartItem == null) { // Create a new cart item if no cart item exists. cartItem = new shoppingCart { productID = id, quantity = 1, userID = userdata.userID, shoppingCartExpiredDate = DateTime.Now.AddDays(7), user = DB.users.SingleOrDefault(u => u.userID == userdata.userID), product = DB.products.SingleOrDefault(p => p.productID == id) }; DB.shoppingCarts.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity. cartItem.quantity++; } DB.SaveChanges(); }
private void loadCogiggnee() { using (WebDL.Team2_BookDBEntities bke = new Team2_BookDBEntities()) { //int id =bke.users.Find(Session.["userId"]); var data = from ur in bke.users where ur.userID == userId select ur; WebDL.user user = data.First(); lbCustomerName.Text = user.userName; lbDeliveryAddress.Text = user.userAddress; lbContactNumber.Text = user.userPhone.ToString(); } }
public void AddToCart(string name) { ShoppingCartActions Action = new ShoppingCartActions(); Team2_BookDBEntities DB = new Team2_BookDBEntities(); // Retrieve the product from the database. string ShoppingCartId = Action.GetCatrId(); var user = from u in DB.users where u.userName == ShoppingCartId select u; WebDL.user userdata = user.FirstOrDefault(); var book = from b in DB.products where b.productName == name select b; WebDL.product bookdata = book.FirstOrDefault(); var cartItem = DB.shoppingCarts.FirstOrDefault( c => c.user.userName == ShoppingCartId && c.product.productName == name); var product = DB.products.FirstOrDefault(p => p.productName == name); if (cartItem == null) { // Create a new cart item if no cart item exists. cartItem = new shoppingCart { productID = bookdata.productID, quantity = Convert.ToInt32(tbQuantity.Text), userID = userdata.userID, shoppingCartExpiredDate = DateTime.Now.AddDays(7), user = DB.users.SingleOrDefault(u => u.userID == userdata.userID), product = DB.products.SingleOrDefault(p => p.productName == name) }; DB.shoppingCarts.Add(cartItem); } else { // If the item does exist in the cart, // then add one to the quantity. cartItem.quantity += Convert.ToInt32(tbQuantity.Text); } product.productQty -= Convert.ToInt32(tbQuantity.Text); DB.SaveChanges(); }
protected void btnAddToCart_Click(object sender, EventArgs e) { Button btn = (Button)sender; string productName = btn.CssClass; using (WebDL.Team2_BookDBEntities context = new WebDL.Team2_BookDBEntities()) { string userNameFromSession = Session["userName"].ToString(); var data = from p in context.products where p.productName == productName select p; var data2 = from u in context.users where u.userName == userNameFromSession select u; WebDL.user userGetFromSession = data2.First(); WebDL.product pToBeAdded = data.First(); WebDL.shoppingCart sc = new WebDL.shoppingCart(); sc.product = pToBeAdded; sc.quantity = 1; sc.userID = userGetFromSession.userID; sc.shoppingCartExpiredDate = DateTime.Now.Date; context.shoppingCarts.Add(sc); context.SaveChanges(); lbMsg.Text = "product: " + productName + " is added to shopping cart"; } }