protected void delete_Click(object sender, EventArgs e) { int eq_id = Convert.ToInt32(TextBox1.Text); int result = 0; string fileName = "~/images/"; try { equipment delete = myDBEntities.equipment.Single(id => id.eq_id == eq_id); fileName += delete.picture; myDBEntities.equipment.Remove(delete); result = myDBEntities.SaveChanges(); } catch (Exception ex) { Response.Write("<script>window.onload = function () {alert('删除失败,编号不存在');}</script>"); } if (result > 0) { //文件是否存在 if (File.Exists(fileName)) { //删除文件 File.Delete(fileName); } Response.Redirect("~/WebForm1/WebForm1-3.aspx"); } }
protected void Button2_Click(object sender, EventArgs e) { //获取输入内容 string name = TextBox3.Text; if (name != "") { //创建实体类对象 department department = new department(); department.name = name; //通过linq技术添加对象到数据库 myDBEntities myDBEntities = new myDBEntities(); myDBEntities.department.Add(department); int result = myDBEntities.SaveChanges(); //通过执行返回值判断是否添加成功 if (result > 0) { Label1.Text = "添加成功"; } else { Label1.Text = "添加失败"; } } else { Label1.Text = "请输入添加的信息!"; } }
public ActionResult Index(register model) { if (ModelState.IsValid) { try { var DeleteRecord = db.registers.Where(x => x.EventName == model.EventName).Where(i => i.Email == model.Email).ToList(); if (DeleteRecord.Count() > 0) { TempData["duplicate"] = "Error: Event Data already registered under this client"; ViewBag.EventName = new SelectList(db.events, "EventName", "EventName"); ViewBag.Email = new SelectList(db.clients, "Email", "Email"); return(View()); //return RedirectToAction("Index"); } db.registers.Add(model); db.SaveChanges(); ModelState.Clear(); TempData["msg"] = "Event Data registered successfully"; db.Dispose(); return(RedirectToAction("Index")); } catch { TempData["duplicate"] = "Something went wrong!, please verify your network connectivity"; return(View()); } } else { TempData["duplicate"] = "Inserted data not valid. Please recheck your data and register again"; return(View()); } }
// GET: Home public ActionResult MockCategories() { myDBEntities myDB = new myDBEntities(); List <Category> categories = myDB.Category.ToList(); List <String> listOfCategories = new List <string> { "Food", "Beverages", "Other" }; Category newCategory = new Category(); if (categories == null) { try { foreach (string category in listOfCategories) { newCategory.Name = category; myDB.Category.Add(newCategory); } myDB.SaveChanges(); } catch (Exception ex) { throw ex; } } return(RedirectToAction("Index", "Product")); }
public ActionResult Edit(Product editProduct) { myDBEntities myDB = new myDBEntities(); Product product = myDB.Product.Single(x => x.Id == editProduct.Id); List <Category> categories = myDB.Category.ToList(); ViewBag.Categories = new SelectList(categories, "Id", "Name"); try { product.Name = editProduct.Name.Trim(); product.Price = editProduct.Price; product.Modified = DateTime.Now; product.Created = product.Created; product.IsActive = true; product.CategoryId = editProduct.CategoryId; myDB.SaveChanges(); } catch (DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); raise = new InvalidOperationException(message, raise); } } throw raise; } return(RedirectToAction("Index")); }
public ActionResult Index(client model) { if (ModelState.IsValid) { try { var mydb = new myDBEntities(); if (mydb.clients.Any(exist => exist.Email == model.Email)) { TempData["duplicate"] = "Duplicate Email found!, please recheck your client's email"; return(View()); } mydb.clients.Add(model); mydb.SaveChanges(); ModelState.Clear(); TempData["msg"] = "Client data registred successfully!"; mydb.Dispose(); return(RedirectToAction("Index")); } catch { TempData["duplicate"] = "Something went wrong!, please verify your network connectivity"; return(View()); } } else { TempData["duplicate"] = "Inserted data not valid. Please recheck your data and register again"; return(View()); } }
public ActionResult Create(HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { string ImageName = System.IO.Path.GetFileName(file.FileName); string path = Server.MapPath("~/Images/" + ImageName); file.SaveAs(path); ImageUploader uploads = new ImageUploader(); uploads.Avatar = ImageName; uploads.Email = Request.Form["Email"]; uploads.Name = Request.Form["Name"]; db.ImageUploaders.Add(uploads); db.SaveChanges(); } return(RedirectToAction("Index")); }
protected void Button2_Click(object sender, EventArgs e) { //获取输入内容 string name = TextBox3.Text; string specifications = TextBox4.Text; string price = TextBox5.Text; string date = TextBox6.Text; string location = TextBox7.Text; string picture = ""; // 重新命名文件名,避免重复和保密 string exten = System.IO.Path.GetExtension(FileUpload1.FileName); //取扩展名 string newFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + exten; //生成新的文件名 string path = "~/images/" + newFileName; //设置上传的目标虚拟路径 if (FileUpload1.HasFile) //如果上传文件控件不为空,既有文件 { picture = newFileName; } if (name != "" && specifications != "" && price != "" && date != "" && picture != "" && location != "") { //创建实体类对象 并且赋值 equipment equipment = new equipment(); equipment.name = name; equipment.specifications = specifications; equipment.price = Convert.ToDouble(price); equipment.date = Convert.ToDateTime(date); equipment.location = location; equipment.picture = picture; equipment.ep_id = Convert.ToInt32(Session["id"]); //通过linq技术 把实体类对象添加到数据库中 myDBEntities myDBEntities = new myDBEntities(); myDBEntities.equipment.Add(equipment); int result = myDBEntities.SaveChanges(); //通过执行添加操作的返回值 判断添加是否成功 if (result > 0) { Label1.Text = "添加成功"; FileUpload1.SaveAs(Server.MapPath(path));//保存图片 } else { Label1.Text = "添加失败"; } } else { Label1.Text = "请输入添加的信息!"; } }
protected void delete_Click(object sender, EventArgs e) { int dp_id = Convert.ToInt32(TextBox1.Text); int result = 0; try { department delete = myDBEntities.department.Single(id => id.dp_id == dp_id); myDBEntities.department.Remove(delete); result = myDBEntities.SaveChanges(); } catch (Exception ex) { Response.Write("<script>window.onload = function () {alert('删除失败,编号不存在');}</script>"); } if (result > 0) { Response.Redirect("~/WebForm3/WebForm3-3.aspx"); } }
protected void Button2_Click(object sender, EventArgs e) { //获取输入的信息 string password = TextBox3.Text; string phone = ""; string name = TextBox4.Text; bool super = checkbox.Checked; string dp_id = DropDownList1.SelectedValue; //判断手机号是否为11位 if (TextBox5.Text.Length == 11) { phone = TextBox5.Text; } //判断输入是否为空 if (name != "" && password != "" && phone != "" && dp_id != "") { //创建实体类对象 employee employee = new employee(); employee.password = password; employee.name = name; employee.phone = phone; employee.administrator = super; employee.dp_id = Convert.ToInt32(dp_id); //利用linq技术把实体类对象添加到数据库 myDBEntities myDBEntities = new myDBEntities(); myDBEntities.employee.Add(employee); int result = myDBEntities.SaveChanges(); //通过执行添加操作的返回值判断是否添加成功 if (result > 0) { Label1.Text = "添加成功"; } else { Label1.Text = "添加失败"; } } else { if (phone == "") { Label1.Text = "请正确填写手机号"; } else { Label1.Text = "请输入添加的信息!"; } } }
public ActionResult Index(string Address, string Phone, string Email, double PaymentAmount) { try { var updateRecord = myDB.clients.Where(x => x.Email == Email).FirstOrDefault(); if (updateRecord != null) { var updateregister = myDB.registers.Where(x => x.Email == updateRecord.Email).FirstOrDefault(); if (updateregister != null) { updateregister.PaymentAmount = PaymentAmount; myDB.SaveChanges(); } /// myDB.clients.Attach(updateRecord); //updateRecord.Address = model.Address; //updateRecord.Phone = model.Phone; // myDB.Entry(updateRecord).State = EntityState.Modified; myDB.SaveChanges(); ModelState.Clear(); TempData["msg"] = "Client data updated successfully!"; // myDB.Dispose(); return(RedirectToAction("Index")); } else { TempData["duplicate"] = "Something went wrong!, please verify your network connectivity"; return(RedirectToAction("Index")); } } catch { TempData["duplicate"] = "Something went wrong!, please verify your network connectivity"; return(RedirectToAction("Index")); } }
//brisanje proizvoda public ActionResult Delete(int Id) { myDBEntities myDB = new myDBEntities(); Product product = myDB.Product.Single(x => x.Id == Id && x.IsActive == true); if (product != null) { product.IsActive = false; try { myDB.SaveChanges(); } catch (Exception ex) { throw ex; } } return(RedirectToAction("Index", "Product")); }
public ActionResult Index(register model) { try { var DeleteRecord = myDB.registers.Where(x => x.EventName == model.EventName).Where(i => i.Email == model.Email).ToList(); foreach (var i in DeleteRecord) { myDB.registers.Remove(i); } //myDB.registers.Remove(DeleteRecord); myDB.SaveChanges(); ModelState.Clear(); TempData["msg"] = "Event data deleted successfully!"; // myDB.Dispose(); return(RedirectToAction("Index")); } catch { TempData["duplicate"] = "Something went wrong!, please verify data provided or contact your system administrator"; myDB.Dispose(); return(RedirectToAction("Index")); } }