public ActionResult AddToOrders([Bind(Include = "OrderID,status,CustomerID,Total")] Order order, StockViewModel svm) { //try //{ DateTime today = DateTime.Now.Date; int stockid = Convert.ToInt16(svm.StockID); int quantity = Convert.ToInt16(svm.QuantityWanted); var cropvar = db.Stocks.Where(x => x.StockID == stockid).Select(y => y.CropID).First(); int cropid = Convert.ToInt16(cropvar); var cropname = db.CropInfoes.Where(x => x.CropID == cropid).Select(y => y.Name).First(); var price = db.Stocks.Where(a => a.StockID == stockid).Select(b => b.Price).Single(); bool check = svm.lowQuantitycheck(quantity); if (check == true) { string name = User.Identity.Name; var getAcctId = (from x in db.Accounts where x.UserName == name select x.AccountID).First(); int accid = Convert.ToInt16(getAcctId); var getCustId = (from x in db.Customers where x.AccountID == accid select x.CustomerID).First(); int customerID = Convert.ToInt16(getCustId); order.CustomerID = customerID; var findCust = db.Customers.Where(a => a.CustomerID == order.CustomerID).Select(b => b.CustomerID).Count(); var checkCustOrders = db.Orders.Where(a => a.CustomerID == order.CustomerID).Select(b => b.OrderID).Count(); var oDate = db.Orders.Where(a => a.CustomerID == order.CustomerID).OrderByDescending(x => x.OrderID).Select(b => b.DateOfOrder).Count(); // DateTime orderDate = Convert.ToDateTime(oDate); order.DateOfOrder = today; if (checkCustOrders > 0 && oDate > 0) { var oId = db.Orders.Where(a => a.CustomerID == order.CustomerID).OrderByDescending(x => x.OrderID).Select(b => b.OrderID).First(); var total = db.Orders.Where(a => a.CustomerID == order.CustomerID).OrderByDescending(x => x.OrderID).Select(b => b.Total).First(); order.OrderID = Convert.ToInt16(oId); order.Total = (Convert.ToDouble(price) * quantity) + Convert.ToDouble(total); order.status = "Pending"; db.Entry(order).State = EntityState.Modified; db.SaveChanges(); } else { order.Total = (Convert.ToDouble(price)) * quantity; order.status = "Pending"; db.Orders.Add(order); db.SaveChanges(); } int stockID = Convert.ToInt16(stockid); var subPrice = db.Stocks.Where(x => x.StockID == stockID).Select(s => s.Price).Single(); StockOrder so = new StockOrder(); so.StockID = stockID; so.OrderID = order.OrderID; so.Quantity = quantity; so.SubPrice = Convert.ToInt16(subPrice); StockOrdersController soc = new StockOrdersController(); soc.AddStockOrders(so); Stock st = new Stock(); st.stockDeduct(so.Quantity, stockid); TempData["SuccessMessage"] = quantity + " " + cropname + " successfully added to orders"; return(RedirectToAction("Store", "Stocks")); } else { TempData["lessQuantityMessage"] = "Selected Quantity not available"; return(RedirectToAction("Store", "Stocks")); } //} //catch //{ // return Content("Failed"); //} }