private string checkout() { var currentUserId = HttpContext.Session.GetInt32("UserId"); var cart = _db.Cart.Where(x => x.UserId.Equals(currentUserId)).FirstOrDefault(); var number_generator = new Random(); var cartItems = _db.CartItem.Where(x => x.ShoppingCartId.Equals(cart.CartId)).ToList(); var OrderID = number_generator.Next(100000, 999999); foreach (var x in cartItems) { var addOrder = new OrderDetails { ProductId = x.ProductId, Quantity = x.Quantity, UnitPrice = x.ProductPrice, CustomerID = cart.UserId, Discount = 0, TransactionId = OrderID, ProductName = x.ProductTitle }; _db.OrderDetails.Add(addOrder); _db.SaveChanges(); _db.Entry <OrderDetails>(addOrder).State = EntityState.Detached; } var connectionStringBuilder = new SqliteConnectionStringBuilder(); connectionStringBuilder.DataSource = "./database.db"; var connection = new SqliteConnection(connectionStringBuilder.ConnectionString); connection.Open(); var selectCmd = connection.CreateCommand(); selectCmd.CommandText = $"delete from CartItem where ShoppingCartId = {cart.CartId}"; var result = selectCmd.ExecuteNonQuery(); ViewBag.CheckoutOk = "Successful"; return(ViewBag.CheckoutOk); }
public IHttpActionResult PutProjectImages(int id, ProjectImages projectImages) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != projectImages.Id) { return(BadRequest()); } db.Entry(projectImages).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ProjectImagesExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Save(ProductSold model) { var result = false; try { if (model.Id > 0) //Updating { ProductSold pro = db.ProductSolds.SingleOrDefault(x => x.Id == model.Id); if (model.ProductId == 0) //Changes not made for this field --- put a check here isempty isnull (value will be sent null from form if it has not been changed) if valkue is changed in form modal it will apear here { pro.ProductId = pro.ProductId; } else { pro.ProductId = model.ProductId; } if (model.CustomerId == 0) { pro.CustomerId = pro.CustomerId; } else { pro.CustomerId = model.CustomerId; } if (model.StoreId == 0) { pro.StoreId = pro.StoreId; } else { pro.StoreId = model.StoreId; } pro.DateSold = model.DateSold; db.Entry(pro).State = EntityState.Modified; db.SaveChanges(); result = true; } else //Adding { ProductSold pro = new ProductSold(); pro.ProductId = model.ProductId; pro.CustomerId = model.CustomerId; pro.StoreId = model.StoreId; pro.DateSold = model.DateSold; db.ProductSolds.Add(pro); db.SaveChanges(); result = true; } } catch (Exception ex) { throw ex; } return(Json(result, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit([Bind(Include = "id,nombre,edad,sexoId")] persona persona) { if (ModelState.IsValid) { db.Entry(persona).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.sexoId = new SelectList(db.sexos, "id", "Nsexo", persona.sexoId); return(View(persona)); }
public async Task <IActionResult> PutAd(int id, AdEntity adEntity) { if (id != adEntity.Id) { } _context.Entry(adEntity).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AdEntityExists(id)) { } else { throw; } } return(null); }
public void Update(T obj) { context.Entry(obj).State = Microsoft.EntityFrameworkCore.EntityState.Modified; context.SaveChanges(); }
public ActionResult PlaceOrder(Order order) { using (var transaction = _context.Database.BeginTransaction()) { try { int challan = 300; int bill = 500; int receipt = 1300; var existchallan = (from u in _context.Orders orderby u.ChallanNo descending select u).Take(1).FirstOrDefault(); if (existchallan != null) { challan = existchallan.ChallanNo + 1; } if (existchallan != null) { bill = existchallan.BillNo + 1; } if (existchallan != null) { receipt = existchallan.ReceiptNo + 1; } var orderObj = new Order { Id = 0, OfferId = order.OfferId, Quantity = order.Quantity, Remarks = order.Remarks, ChallanNo = challan, BillNo = bill, ReceiptNo = receipt, PaymentType = order.PaymentType, OrderDate = DateTime.Now }; _context.Orders.Add(orderObj); _context.SaveChanges(); var offer = _context.Offers.Find(orderObj.OfferId); Order ordervm = new Order(); ordervm = _context.Orders.Find(orderObj.Id); ordervm.TotalPrice = offer.Price * orderObj.Quantity; _context.Entry(ordervm).State = EntityState.Modified; _context.SaveChanges(); transaction.Commit(); return(RedirectToAction("GetOrder")); } catch (Exception) { transaction.Rollback(); return(View()); } } }