public ActionResult Create([Bind(Include = "ProductID,Name,ProductNumber,Color,StandardCost,ListPrice,Size,Weight,ProductCategoryID,ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,ThumbNailPhoto,ThumbnailPhotoFileName,rowguid,ModifiedDate")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ProductCategoryID = (IEnumerable <SelectListItem>) from cat in db.ProductCategories where cat.ParentProductCategoryID == 1 select new SelectListItem { Value = cat.ProductCategoryID.ToString(), Text = cat.Name }; ViewBag.ProductModelID = (from mod in db.vProductAndDescription2 where mod.ProductCategoryID == 5 || mod.ProductCategoryID == 6 || mod.ProductCategoryID == 7 select new SelectListItem { Value = mod.ProductModelID.ToString(), Text = mod.ProductModel }).Distinct(); ViewBag.MountainModels = (from mod in db.vProductAndDescription2 where mod.ProductCategoryID == 5 select new SelectListItem { Value = mod.ProductModelID.ToString(), Text = mod.ProductModel }).Distinct(); ViewBag.RoadModels = (from mod in db.vProductAndDescription2 where mod.ProductCategoryID == 6 select new SelectListItem { Value = mod.ProductModelID.ToString(), Text = mod.ProductModel }).Distinct(); ViewBag.TouringModels = (from mod in db.vProductAndDescription2 where mod.ProductCategoryID == 7 select new SelectListItem { Value = mod.ProductModelID.ToString(), Text = mod.ProductModel }).Distinct(); ViewBag.now = DateTime.Now.Date.ToString("yyyy/MM/dd"); return(View(product)); }
public ActionResult Create([Bind(Include = "ProductID,Name,ProductNumber,Color,StandardCost,ListPrice,Size,Weight,ProductCategoryID,ProductModelID,SellStartDate,SellEndDate,DiscontinuedDate,ThumbNailPhoto,ThumbnailPhotoFileName,rowguid,ModifiedDate")] Product product, HttpPostedFileBase uploadFile) { if (ModelState.IsValid) { product.ModifiedDate = System.DateTime.Now; product.rowguid = Guid.NewGuid(); // Manager uploaded an image if (uploadFile != null && uploadFile.ContentLength > 0) { product.ThumbNailPhoto = ConvertImage(uploadFile); product.ThumbnailPhotoFileName = uploadFile.FileName; } // Manager didn't upload anything else { // Seems to be the only functioning way I could find var image = db.Products.FirstOrDefault(i => i.ThumbnailPhotoFileName == "no_image_available_small.gif"); product.ThumbNailPhoto = image.ThumbNailPhoto; product.ThumbnailPhotoFileName = "no_image_available_small.gif"; } db.Products.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } var category = (from x in db.ProductCategories where x.ParentProductCategoryID == 1 select new SelectListItem { Value = x.ProductCategoryID.ToString(), Text = x.Name }).Distinct(); ViewBag.ProductCategory = category; return(View(product)); }
public HttpResponseMessage PostModel([FromBody] ProductModel_min pm) { if (string.IsNullOrEmpty(pm.Name)) { throw new HttpResponseException(HttpStatusCode.NotAcceptable); } else { ProductModel newPm = new ProductModel(); newPm.ProductModelID = tools.NewModelID(); newPm.Name = pm.Name; newPm.CatalogDescription = pm.CatalogDescription; newPm.Instructions = pm.Instructions; newPm.rowguid = pm.rowguid; newPm.ModifiedDate = DateTime.Now; newPm.Product = null; db.ProductModel.Add(newPm); db.SaveChanges(); var response = Request.CreateResponse <ProductModel>(HttpStatusCode.Created, newPm); return(response); } }
public IHttpActionResult PutSalesTerritory(int id, SalesTerritory salesTerritory) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != salesTerritory.TerritoryID) { return(BadRequest()); } db.Entry(salesTerritory).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!SalesTerritoryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public ActionResult Create([Bind(Include = "ProductCategoryID,ParentProductCategoryID,Name,rowguid,ModifiedDate")] ProductCategory productCategory) { if (ModelState.IsValid) { db.ProductCategories.Add(productCategory); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ParentProductCategoryID = new SelectList(db.ProductCategories, "ProductCategoryID", "Name", productCategory.ParentProductCategoryID); return(View(productCategory)); }
public ActionResult Create([Bind(Include = "BusinessEntityID,NationalIDNumber,LoginID,OrganizationLevel,JobTitle,BirthDate,MaritalStatus,Gender,HireDate,SalariedFlag,VacationHours,SickLeaveHours,CurrentFlag,rowguid,ModifiedDate")] Employee employee) { if (ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.BusinessEntityID = new SelectList(db.People, "BusinessEntityID", "PersonType", employee.BusinessEntityID); ViewBag.BusinessEntityID = new SelectList(db.SalesPersons, "BusinessEntityID", "BusinessEntityID", employee.BusinessEntityID); return(View(employee)); }
public HttpResponseMessage PostProduct([FromBody] Product p) { if (string.IsNullOrEmpty(p.Name)) { throw new HttpResponseException(HttpStatusCode.NotAcceptable); } else { p.ProductID = tools.NewProductID(); db.Product.Add(p); db.SaveChanges(); var response = Request.CreateResponse <Product>(HttpStatusCode.Created, p); return(response); } }
private void Insert_Click(object sender, EventArgs e) { try { using (var context = new AdventureWorks2012Entities()) { var busent = context.BusinessEntities.Where(p => p.BusinessEntityID == 292).First(); var per = new Person(); per.PersonType = "SC"; per.NameStyle = true; per.Title = "Geek"; per.FirstName = "Scott"; per.MiddleName = "L"; per.LastName = "Klein"; per.Suffix = "Mr"; per.EmailPromotion = 1; per.rowguid = Guid.NewGuid(); per.ModifiedDate = DateTime.Now; busent.Person = per; context.SaveChanges(); MessageBox.Show("record inserted"); } } catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); } }
public HttpResponseMessage Put(Employee employee) { try { BusinessEntity businessEntity = new BusinessEntity(); Person person = employee.Person; using (var db = new AdventureWorks2012Entities()) { businessEntity.rowguid = Guid.NewGuid(); businessEntity.ModifiedDate = DateTime.Now; db.BusinessEntity.Add(businessEntity); person.rowguid = Guid.NewGuid(); person.BusinessEntity = businessEntity; person.BusinessEntityID = businessEntity.BusinessEntityID; person.ModifiedDate = DateTime.Now; db.Person.Add(person); employee.rowguid = Guid.NewGuid(); employee.BusinessEntityID = person.BusinessEntityID; employee.ModifiedDate = DateTime.Now; db.Employee.Add(employee); db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.Created)); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.Conflict)); } }
public ActionResult Create([Bind(Include = "ProductID,Name,Rating,Review1")] Review review) { if (ModelState.IsValid) { if (review.Name == null) { review.Name = "Anonymous"; } db.Reviews.Add(review); db.SaveChanges(); return(RedirectToAction("Details", "Products", new { id = review.ProductID })); } ViewBag.ProductID = review.ProductID; ViewBag.Rating = review.Rating; ViewBag.Message = review.Review1; return(View(review)); }
public void SetClienteById(Customer editCliente) { var cliente = context.Customer.Find(editCliente.CustomerID); if (cliente != null) { cliente.PersonID = editCliente.PersonID; cliente.StoreID = editCliente.StoreID; cliente.TerritoryID = editCliente.TerritoryID; cliente.AccountNumber = editCliente.AccountNumber; cliente.ModifiedDate = editCliente.ModifiedDate; context.SaveChanges(); } else { throw new NullReferenceException("Customer not found"); } }
public void SetCustomersById(Customer editCustomer) { var Customer = context.Customer.Find(editCustomer.CustomerID); if (Customer != null) { Customer.PersonID = editCustomer.PersonID; Customer.StoreID = editCustomer.StoreID; Customer.TerritoryID = editCustomer.TerritoryID; Customer.AccountNumber = editCustomer.AccountNumber; Customer.ModifiedDate = editCustomer.ModifiedDate; context.SaveChanges(); } else { throw new NullReferenceException("Customer not found"); } }
public void SetProductPrice(Product p) { var _db = new AdventureWorks2012Entities(); var product = _db.Products.Where(pr => pr.ProductID == p.ProductID).FirstOrDefault(); product.ListPrice = p.ListPrice; TryUpdateModel(product); if (ModelState.IsValid) { _db.SaveChanges(); } }
// PUT: odata/Employees(5) public IHttpActionResult Put([FromODataUri] int key, Delta <Employee> patch) { Validate(patch.GetEntity()); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Employee employee = db.Employee.Find(key); if (employee == null) { return(NotFound()); } patch.Put(employee); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!EmployeeExists(key)) { return(NotFound()); } else { throw; } } return(Updated(employee)); }
public HttpResponseMessage Post(Employee employee) { try { using (var db = new AdventureWorks2012Entities()) { db.Entry(employee).State = EntityState.Modified; db.Entry(employee.Person).State = EntityState.Modified; db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.Conflict)); } }
private void ApplyChangesToMultiple() { using (var context = new AdventureWorks2012Entities()) { var people = from p in context.People where p.LastName == "King" orderby p.LastName, p.FirstName descending select p; foreach (var person in people) { person.FirstName = person.FirstName + " - 2"; } context.SaveChanges(); } }
private void RevertChangesFromMultiple() { using (var context = new AdventureWorks2012Entities()) { var people = from p in context.People where p.LastName == "King" orderby p.LastName, p.FirstName descending select p; foreach (var person in people) { var firstName = person.FirstName; person.FirstName = firstName.Substring(0, firstName.Length - 4); } context.SaveChanges(); } }
private void Delete_Click(object sender, EventArgs e) { try { using (var context = new AdventureWorks2012Entities()) { var per = context.People.Where(p => p.BusinessEntityID == 292).First(); context.DeleteObject(per); context.SaveChanges(); MessageBox.Show("record deleted"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void Update_Click(object sender, EventArgs e) { try { using (var context = new AdventureWorks2012Entities()) { var per = context.People.Where(p => p.BusinessEntityID == 292).First(); per.Title = "Head Geek"; per.ModifiedDate = DateTime.Now; per.PersonType = "EM"; context.SaveChanges(); MessageBox.Show("record updated"); } } catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); } }
public HttpResponseMessage Delete(Employee employee) { try { using (var db = new AdventureWorks2012Entities()) { var query = from e in db.Employee where e.BusinessEntityID == employee.BusinessEntityID orderby e.BusinessEntityID select e; foreach (var employeeToDelete in query) { employeeToDelete.CurrentFlag = false; } db.SaveChanges(); } return(Request.CreateResponse(HttpStatusCode.OK)); } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.Conflict)); } }
public PartialViewResult Edit(Product product) { try { if (ModelState.IsValid) { var editedProduct = db.Products.Find(product.ProductID); editedProduct.Color = product.Color; db.Entry(editedProduct).State = EntityState.Modified; db.SaveChanges(); return(PartialView("_EditParital", editedProduct)); } } catch (DataException dex) { //Log the error (uncomment dex variable name and add a line here to write a log. ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } var oldProduct = db.Products.Find(product.ProductID); return(PartialView("_EditPartial", oldProduct)); }
public ActionResult addCustomer(Customer cliente) { context.Customer.Add(cliente); context.SaveChanges(); return(View()); }
public void Save() { context.SaveChanges(); }