/// <summary> /// Inserting new rows into database /// </summary> /// <param name="rows"></param> protected void InsertRows(IEnumerable <Car> rows) { using (SampleDatabaseEntities db = new SampleDatabaseEntities()) { try { foreach (var item in rows) { CarsTable carsTable = new CarsTable(); carsTable.Factory = item.Factory; carsTable.Model = item.Model; carsTable.Warranty = item.Warranty; carsTable.ModelYear = DateTime.Now; db.CarsTables.Add(carsTable); } db.SaveChanges(); StatusLabel.Text = "Import success!"; } catch (Exception ex) { StatusLabel.Text = "Problem with import: " + ex.Message; } } }
public void addCar_InsertItem() { var item = new CarsTable(); TryUpdateModel(item); if (ModelState.IsValid) { using (SampleDatabaseEntities db = new SampleDatabaseEntities()) { db.CarsTables.Add(item); db.SaveChanges(); } } }
/// <summary> /// Deleting item /// </summary> /// <param name="Id"></param> public void carsGrid_DeleteItem(int Id) { using (SampleDatabaseEntities db = new SampleDatabaseEntities()) { var item = new CarsTable { Id = Id }; db.Entry(item).State = EntityState.Deleted; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { ModelState.AddModelError("", String.Format("Delete issue: item id {0} not found in database.", Id)); } } }
/// <summary> /// Update item /// </summary> /// <param name="Id"></param> public void carsGrid_UpdateItem(int Id) { using (SampleDatabaseEntities db = new SampleDatabaseEntities()) { CarsTable item = null; item = db.CarsTables.Find(Id); if (item == null) { ModelState.AddModelError("", String.Format("Update issue: item id {0} not found in database.", Id)); return; } TryUpdateModel(item); if (ModelState.IsValid) { db.SaveChanges(); } } }