private void btnSave_Click(object sender, EventArgs e) { if (this.address.AddressId == 0) { ctx.Addresses.Add(this.address); ctx.SaveChanges(); this.Close(); } ctx.Entry(this.address).CurrentValues.SetValues(this.address); ctx.SaveChanges(); this.Close(); }
public ActionResult AddDataToTest() { var cloudTableClient = cloudStorageAccount.CreateCloudTableClient(); var context = new CustomerDataContext(cloudStorageAccount.TableEndpoint.AbsoluteUri, cloudStorageAccount.Credentials); try { // 向表中添加65个实体 for (int i = 0; i < 65; i++) { context.AddObject("Customers", new Customer() { Age = r.Next(16, 70), Name = "Customer" + i.ToString() }); } context.SaveChanges(); MVCSessionCachedDataProvider <Customer> provider = new MVCSessionCachedDataProvider <Customer>(this, "provider1"); TableStoragePagingUtility <Customer> pagingUtility = new TableStoragePagingUtility <Customer>(provider, cloudStorageAccount, context, 10, "Customers"); return(View("Index", new CustomersSet() { Customers = pagingUtility.GetNextPage().ToList(), ReadyToShowUI = true })); } catch (Exception ex) { return(View("Error", new HandleErrorInfo(ex, "HomeController", "AddDataToTest"))); } }
public CustomerServiceFixture() { // Set up in memory db options scoped to current test fixture DbContextOptions <CustomerDataContext> options; var builder = new DbContextOptionsBuilder <CustomerDataContext>(); builder.UseInMemoryDatabase(nameof(CustomerServiceFixture)); options = builder.Options; // Create customer db _testDataContext = new CustomerDataContext(options); _testDataContext.Database.EnsureDeleted(); _testDataContext.Database.EnsureCreated(); //Seed with one test record _testRecordGuid = Guid.NewGuid(); _testDataContext.Add(new Customer { Id = _testRecordGuid, FirstName = "Daniel", LastName = "Simpson", DateOfBirth = new DateTime(1988, 1, 5) }); _testDataContext.SaveChanges(); //Create test CustomerService _testCustomerService = new CustomerService(_testDataContext); }
private void UpdateButton_Click(object sender, EventArgs e) { var updateAddress = customerDataContext.Addresses.Find(dataRow.Cells["AddressID"].Value); updateAddress.Street = streetBox.Text; updateAddress.City = cityBox.Text; updateAddress.State = stateBox.Text; updateAddress.Zip = zipBox.Text; customerDataContext.SaveChanges(); Application.OpenForms.OfType <Customers>().FirstOrDefault().GetCustomers(); this.Close(); }
//Insert public List <IndividualCustomer> POST(IndividualCustomer customer) { CustomerDataContext customerDataAccess = new CustomerDataContext(); if (ModelState.IsValid) { //Save Customer customerDataAccess.Customers.Add(customer); customerDataAccess.SaveChanges(); } List <IndividualCustomer> customers = customerDataAccess.Customers.ToList <IndividualCustomer>(); return(customers); }
public ActionResult Create(CustomerDataViewModel model) { if (ModelState.IsValid) { if (model.File != null) { //string pic = string.Concat(String.Format("{0:s}", DateTime.Now), Path.GetFileName(model.File.FileName)); string pic = Path.GetFileName(model.File.FileName); string dirPath = Server.MapPath("~/images/"); if (!System.IO.Directory.Exists(dirPath)) { System.IO.Directory.CreateDirectory(dirPath); } string path = Path.Combine( Server.MapPath("~/images"), pic); //Save the image model.File.SaveAs(path); using (MemoryStream ms = new MemoryStream()) { model.File.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); } model.CustomerDatas.Image = new Image(); model.CustomerDatas.Image.ImageName = model.File.FileName; } db.CustomerDatas.Add(model.CustomerDatas); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(model)); }
//Update public List <IndividualCustomer> Delete(IndividualCustomer customer) { CustomerDataContext customerDataAccess = new CustomerDataContext(); //Delete Customer IndividualCustomer customerDelete = (from item in customerDataAccess.Customers where item.CustomerCode == customer.CustomerCode select item).FirstOrDefault(); customerDataAccess.Customers.Remove(customerDelete); customerDataAccess.SaveChanges(); //Return Customer List <IndividualCustomer> customers = customerDataAccess.Customers.ToList <IndividualCustomer>(); return(customers); }
private void SaveChanges_Click(object sender, EventArgs e) { //update the address from the textboxes on the UI var changed = ctx.Addresses.Where(c => c.CustomerId == IDUsed).FirstOrDefault(); changed.Street = StreetInOut.Text; changed.City = CityInOut.Text; changed.State = StateInOut.Text; changed.Zip = ZipInOut.Text; ctx.SaveChanges(); //return to the Main form Main returntomain = new Main(); returntomain.Show(); this.Close(); }
private void updatebtn_Click(object sender, EventArgs e) { try { SqlParameter name = new SqlParameter("Firstname", firstnametxt.Text); SqlParameter id = new SqlParameter("CustomerID", Convert.ToInt32(drow.Cells["CustomerID"].Value)); ctx.Database.ExecuteSqlRaw("EXECUTE updateCustomers @Firstname, @CustomerID", name, id); //execute stored proc ctx.SaveChanges(); System.Windows.Forms.Application.OpenForms.OfType <CustomerRead>().FirstOrDefault().getData(); //refreshes main form this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
private void AddrUpdate_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(StreetBox.Text) || string.IsNullOrWhiteSpace(StreetBox.Text) || string.IsNullOrEmpty(CityBox.Text) || string.IsNullOrWhiteSpace(CityBox.Text) || string.IsNullOrEmpty(StateBox.Text) || string.IsNullOrWhiteSpace(StateBox.Text) || string.IsNullOrEmpty(ZipBox.Text) || string.IsNullOrWhiteSpace(ZipBox.Text)) { Required.Visible = true; } else { Required.Visible = false; Address NewAddr = CDC.Addresses.Single(a => a.CustomerId == CustomerID); NewAddr.Street = StreetBox.Text; NewAddr.City = CityBox.Text; NewAddr.State = StateBox.Text; NewAddr.Zip = ZipBox.Text; CDC.SaveChanges(); this.Close(); } }
private void updatebtn_Click(object sender, EventArgs e) { try { var update = ctx.Addresses.Find(drow.Cells["AddressID"].Value); update.Street = streettxt.Text; update.City = citytxt.Text; update.State = statetxt.Text; update.Zip = ziptxt.Text; ctx.Entry(update).State = Microsoft.EntityFrameworkCore.EntityState.Modified; //update any changes made ctx.SaveChanges(); System.Windows.Forms.Application.OpenForms.OfType <CustomerRead>().FirstOrDefault().getData(); //refreshes main form this.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
private void button1_Click(object sender, EventArgs e) { dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Selected = true; var newUser = cdtx.Addresses.First(c => c.CustomerId == userId); var thisUser = dataGridView1.SelectedRows; foreach (DataGridViewRow cell in thisUser) { if (string.IsNullOrEmpty(streetBox1.Text.ToString())) { } else { newUser.Street = streetBox1.Text.ToString(); } if (string.IsNullOrEmpty(cityBox.Text.ToString())) { } else { newUser.City = cityBox.Text.ToString(); } if (string.IsNullOrEmpty(stateBox.Text.ToString())) { } else { newUser.State = stateBox.Text.ToString(); } if (string.IsNullOrEmpty(zipBox.Text.ToString())) { } else { newUser.Zip = zipBox.Text.ToString(); } } cdtx.SaveChanges(); updateScreen(); }
private void updateButton_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(streetText.Text) || string.IsNullOrEmpty(cityText.Text) || string.IsNullOrEmpty(stateText.Text) || string.IsNullOrEmpty(zipText.Text)) { MessageBox.Show("No fields may be empty!"); } else if (stateText.Text.Length > 2) { MessageBox.Show("Use state abbreviations please"); } else { CustomerDataContext dc = new CustomerDataContext(); Address updateAddress = dc.Addresses.Single(up => up.CustomerId == PresentCust.id); updateAddress.Street = streetText.Text; updateAddress.City = cityText.Text; updateAddress.State = stateText.Text; updateAddress.Zip = zipText.Text; dc.SaveChanges(); this.Close(); } }
private void Seed(CustomerDataContext customerDataContext) { var customers = new[] { new Customer { Id = 1, Name = "Aubrey Kisar" }, new Customer { Id = 2, Name = "Sloan Battey" }, new Customer { Id = 3, Name = "Marwin Izakson" }, new Customer { Id = 4, Name = "Fonzie Berrington" }, new Customer { Id = 5, Name = "Irwinn Proger" }, new Customer { Id = 6, Name = "Renaud Limeburn" }, new Customer { Id = 7, Name = "Aimil Gilliatt" }, new Customer { Id = 8, Name = "Bradford Corpe" }, new Customer { Id = 9, Name = "Saunders Kapelhoff" }, new Customer { Id = 10, Name = "Gertrud Kelloch" } }; customerDataContext.Customers.AddRange(customers); customerDataContext.SaveChanges(); }
private void UpdateAddressButton_Click(object sender, EventArgs e) { CustomerDataContext cdc = new CustomerDataContext(); if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text) || string.IsNullOrEmpty(textBox4.Text)) { MessageBox.Show("No fields may be empty!"); } else if (textBox3.Text.Length > 2) { MessageBox.Show("Use state abbreviations please"); } else { Address newAddy = cdc.Addresses.Single(upd => upd.CustomerId == ReadOnlyDataForm.currentID); newAddy.Street = textBox1.Text; newAddy.City = textBox2.Text; newAddy.State = textBox3.Text; newAddy.Zip = textBox4.Text; cdc.SaveChanges(); this.Close(); } }
public ActionResult Create(Manufacturer created) { if (!ModelState.IsValid) { return(View(created)); } //make sure that no repeat names are in the database, ToLower because name doesn't need to be all caps var repeatCheck = _db.Manufacturers.FirstOrDefault(manu => manu.ManufacturerName.ToLower() == created.ManufacturerName.ToLower()); if (repeatCheck != null) { ModelState.AddModelError("", "There is already a manufacturer with the same name in the Records!"); return(View(created)); } _db.Manufacturers.Add(created); _db.SaveChanges(); //after create a manufacture redirect to create some models otherwise why was it added, grab last one as id is incremental return(RedirectToAction("CreateModel", "MakeandModel", new { manuId = _db.Manufacturers.Last().ManufacturerId })); }
public ActionResult Add(AddServiceViewModel created) { if (_db.Manufacturers.Any() == false) { return(View("NoManufacturers")); } //if there are no models in db, code still functions on page, but better to give the warning if (_db.DeviceModels.Any() == false) { return(View("NoManufacturers")); } created.ManufacturerList = GenerateManufacturerList(); created.ModelList = GenerateModelList(); if (!ModelState.IsValid) { return(View(created)); } //if the values of is specific is true set values of model and manufacturer to null, or values are if (created.NotSpecific == true) { created.Manufacturer = null; created.Model = null; } bool nameExists = false; // check if the manufacturer and model fields are empty //convert to lowercase so that difference in capitals does not matter if (created.Manufacturer == null && created.Model == null) { nameExists = _db.Services.Any(ser => ser.Name.ToLower() == created.Name.ToLower() && ser.Model == null && ser.Manufacturer == null); } else { nameExists = _db.Services.Any( ser => ser.Name.ToLower() == created.Name.ToLower() && ser.Model == created.Model && ser.Manufacturer == created.Manufacturer); } //check if a entry is already in the database with the same name, for specified model and manufacturer if (nameExists) { ModelState.AddModelError("", "That service already exists in the database!"); return(View(created)); } Service converted = new Service { Name = created.Name, Description = created.Description, Price = created.Price, Manufacturer = created.Manufacturer, Model = created.Model }; _db.Services.Add(converted); _db.SaveChanges(); return(RedirectToAction("Index")); }
public void Post(Customer value) { db.Customer.Add(value); db.SaveChanges(); }
public IActionResult EditNameProfile(long id, CustomerProfileViewModel edited) { //check to see if customer with specified id exists var customerToFind = _db.Customers.FirstOrDefault(x => x.CustomerId == id); if (customerToFind == null) { return(RedirectToAction("List")); } //assign the rest of the parts of the model with existing data so model can be resent edited.Creation = customerToFind.DateCreated; edited.CusId = customerToFind.CustomerId; edited.Phones = _db.PhoneNumbers.Where(p => p.CustomerId == id).ToList(); edited.Emails = _db.Emails.Where(e => e.CustomerId == id).ToList(); edited.Addresses = _db.MailingAddresses.Where(x => x.CustomerId == id).ToList(); //make sure model is valid if (!ModelState.IsValid) { return(View(edited)); } customerToFind.FirstName = edited.Given; customerToFind.LastName = edited.Surname; _db.SaveChanges(); return(RedirectToAction("profile", "customer", new { id = customerToFind.CustomerId })); }