public static int Update(Movie movie) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to update tblMovie updateRow = (from dt in dc.tblMovies where dt.Id == movie.Id select dt).FirstOrDefault(); if (updateRow != null) { updateRow.Title = movie.Title; updateRow.Description = movie.Description; updateRow.Cost = movie.Cost; updateRow.RatingID = movie.RatingID; updateRow.DirectorID = movie.DirectorID; updateRow.FormatID = movie.FormatID; updateRow.InStkQty = movie.InStkQty; updateRow.ImagePath = movie.ImagePath; return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { //get the row i want to delete tblMovie deleteRow = (from dt in dc.tblMovies where dt.Id == id select dt).FirstOrDefault(); if (deleteRow != null) { dc.tblMovies.Remove(deleteRow); return(dc.SaveChanges()); } else { throw new Exception("Row not found"); } } } catch (Exception ex) { throw ex; } }
public static int Insert(out int id, string title, string description, double cost, int ratingId, int directorId, int formatId, int inStkQty, string imagePath) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie newrow = new tblMovie(); newrow.Title = title; newrow.Description = description; newrow.Cost = cost; newrow.RatingId = ratingId; newrow.DirectorId = directorId; newrow.FormatId = formatId; newrow.InStkQty = inStkQty; newrow.ImagePath = imagePath; //giving a new row the next id after existing one or if there is none id equals to 1 newrow.Id = dc.tblMovies.Any() ? dc.tblMovies.Max(dt => dt.Id) + 1 : 1; id = newrow.Id; dc.tblMovies.Add(newrow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Insert(Movie movie) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie tblMovie = new tblMovie(); tblMovie.Title = movie.Title; tblMovie.Description = movie.Description; tblMovie.Cost = movie.Cost; tblMovie.RatingID = movie.RatingID; tblMovie.DirectorID = movie.DirectorID; tblMovie.FormatID = movie.FormatID; tblMovie.InStkQty = movie.InStkQty; tblMovie.ImagePath = movie.ImagePath; tblMovie.Id = dc.tblMovies.Any() ? dc.tblMovies.Max(dt => dt.Id) + 1 : 1; movie.Id = tblMovie.Id; dc.tblMovies.Add(tblMovie); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public int Insert() { try { using (DVDEntities dc = new DVDEntities()) { tblMovie movie = new tblMovie { Description = this.Description, Cost = this.Cost, DirectorId = this.DirectorId, FormatId = this.FormatId, ImagePath = this.ImagePath, Quantity = this.Quantity, RatingId = this.RatingId, Title = this.Title, Id = dc.tblMovies.Any() ? dc.tblMovies.Max(m => m.Id) + 1 : 1 }; this.Id = movie.Id; dc.tblMovies.Add(movie); //Return the rows effected return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Update(int id, string title, string description, double cost, int ratingId, int directorId, int formatId, int inStkQty, string imagePath) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie updaterow = (from dt in dc.tblMovies where dt.Id == id select dt).FirstOrDefault(); updaterow.Title = title; updaterow.Description = description; updaterow.Cost = cost; updaterow.RatingId = ratingId; updaterow.DirectorId = directorId; updaterow.FormatId = formatId; updaterow.InStkQty = inStkQty; updaterow.ImagePath = imagePath; return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie deleterow = dc.tblMovies.FirstOrDefault(dt => dt.Id == id); if (deleterow != null) { // Remove the row dc.tblMovies.Remove(deleterow); // Commit/Save the changes return(dc.SaveChanges()); } else { throw new Exception("Row was not found."); } } } catch (Exception ex) { throw ex; } }
public IHttpActionResult PuttblMovie(int id, tblMovie tblMovie) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != tblMovie.id) { return(BadRequest()); } db.Entry(tblMovie).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!tblMovieExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public void Insert() { using (DVDEntities dc = new DVDEntities()) { //Create a movie tblMovie movie = new tblMovie { Id = -99, Title = "TestMovie", Cost = -99.99m, Quantity = -10, Description = "A test movie", FormatId = -99, DirectorId = -99, ImagePath = "TextMovieImg", RatingId = -99 }; //Insert the row dc.tblMovies.Add(movie); //Commit the changes int rowsInserted = dc.SaveChanges(); //Make sure that one row was inserted Assert.IsTrue(rowsInserted == 1); } }
public void InsertTest() { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie newrow = new tblMovie(); newrow.Id = -99; newrow.Title = "Test Movie"; newrow.Description = "It's just a test"; newrow.Cost = 1.22; newrow.RatingID = -99; newrow.FormatID = -99; newrow.DirectorID = -99; newrow.InStkQty = -99; newrow.ImagePath = "ahhhhhh"; dc.tblMovies.Add(newrow); int results = dc.SaveChanges(); Assert.IsTrue(results == 1); } }
public ActionResult DeleteConfirmed(int id) { tblMovie tblMovie = db.tblMovies.Find(id); db.tblMovies.Remove(tblMovie); db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "id,name,duration")] tblMovie tblMovie) { if (ModelState.IsValid) { db.Entry(tblMovie).State = EntityState.Modified;//check if any change is made in the view to the model data db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblMovie)); }
public void Delete() { using (DVDEntities dc = new DVDEntities()) { tblMovie movie = dc.tblMovies.FirstOrDefault(m => m.Id == -99); dc.tblMovies.Remove(movie); int rowsEffected = dc.SaveChanges(); Assert.IsTrue(rowsEffected == 1); } }
public ActionResult Create([Bind(Include = "id,name,duration")] tblMovie tblMovie) { if (ModelState.IsValid) { db.tblMovies.Add(tblMovie);//collection's add db.SaveChanges(); return(RedirectToAction("Index")); } return(View(tblMovie)); }
public IHttpActionResult GettblMovie(int id) { tblMovie tblMovie = db.tblMovies.Find(id); if (tblMovie == null) { return(NotFound()); } return(Ok(tblMovie)); }
private void button3_Click(object sender, RoutedEventArgs e) { CinemaDataContext Context = new CinemaDataContext(); int i = 0; string[] Strs = File.ReadAllLines("Films.txt"); Context.ExecuteCommand("delete from tblMovie"); do { tblMovie Movie = new tblMovie(); Movie.MovieName = Strs[i++]; Movie.MovieDirector = Strs[i++]; Movie.MovieDuration = Convert.ToInt32(Strs[i++]); Movie.MovieYear = Convert.ToInt32(Strs[i++]); Movie.MinAge = Convert.ToInt32(Strs[i++]); Movie.MainActor = Strs[i++]; Context.tblMovies.InsertOnSubmit(Movie); Context.SubmitChanges(); int MovieID = (from M in Context.tblMovies where M.MovieName == Movie.MovieName select M.MovieID).First(); string[] GenresStrs = Strs[i++].Split(); foreach (string S in GenresStrs) { tblMovieGenre MovieGenre = new tblMovieGenre(); MovieGenre.MovieID = MovieID; MovieGenre.GenreID = (from G in Context.tblGenres where G.GenreName == S select G.GenreID).First(); Context.tblMovieGenres.InsertOnSubmit(MovieGenre); } string[] CountriesStrs = Strs[i++].Split(); foreach (string S in CountriesStrs) { tblMovieCountry MovieCountry = new tblMovieCountry(); MovieCountry.MovieID = MovieID; MovieCountry.CountryID = (from G in Context.tblCountries where G.CountryName == S select G.CountryID).First(); Context.tblMovieCountries.InsertOnSubmit(MovieCountry); } i++; }while (i < Strs.Length); Context.SubmitChanges(); }
public IHttpActionResult PosttblMovie(tblMovie tblMovie) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.tblMovies.Add(tblMovie); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = tblMovie.id }, tblMovie)); }
// DELETE: api/Movie/5 public HttpResponseMessage Delete(HttpRequestMessage message, [FromBody] tblMovie movie) { try { entities.Entry(movie).State = System.Data.Entity.EntityState.Deleted; entities.SaveChanges(); return(message.CreateResponse(HttpStatusCode.Accepted, movie)); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public HttpResponseMessage Something(HttpRequestMessage message, [FromBody] tblMovie movie) { try { entities.tblMovies.Add(movie); entities.SaveChanges(); return(message.CreateResponse(HttpStatusCode.Accepted, movie)); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public IHttpActionResult DeletetblMovie(int id) { tblMovie tblMovie = db.tblMovies.Find(id); if (tblMovie == null) { return(NotFound()); } db.tblMovies.Remove(tblMovie); db.SaveChanges(); return(Ok(tblMovie)); }
// GET: tblMovies/Delete/5 public ActionResult Delete(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } tblMovie tblMovie = db.tblMovies.Find(id); if (tblMovie == null) { return(HttpNotFound()); } return(View(tblMovie)); }
public void Update() { using (DVDEntities dc = new DVDEntities()) { tblMovie movie = dc.tblMovies.FirstOrDefault(m => m.Id == -99); movie.Title = "TestingUpdate"; //Save changed property dc.SaveChanges(); tblMovie updatedMovie = dc.tblMovies.FirstOrDefault(m => m.Title == "TestingUpdate"); Assert.AreEqual(movie.Id, updatedMovie.Id); } }
public static int Delete(int id) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovie deleterow = (from dt in dc.tblMovies where dt.Id == id select dt).FirstOrDefault(); dc.tblMovies.Remove(deleterow); return(dc.SaveChanges()); } } catch (Exception ex) { throw ex; } }
// PUT: api/Movie/5 public HttpResponseMessage Put(HttpRequestMessage message, int id, [FromBody] tblMovie movie) { try { entities.Entry(movie).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); foreach (var item in entities.ChangeTracker.Entries()) { item.Reload(); } return(message.CreateResponse(HttpStatusCode.Accepted, movie)); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } }
public int Update() { try { using (DVDEntities dc = new DVDEntities()) { //Make sure that the ID is set and valid if (this.Id >= 0) { tblMovie movie = dc.tblMovies.FirstOrDefault(m => m.Id == this.Id); //Make sure that a movie was retrieved if (movie != null) { //Update the proterties on the movie movie.Description = this.Description; movie.Cost = this.Cost; movie.DirectorId = this.DirectorId; movie.FormatId = this.FormatId; movie.ImagePath = this.ImagePath; movie.Quantity = this.Quantity; movie.RatingId = this.RatingId; movie.Title = this.Title; //return the number of rows effected return(dc.SaveChanges()); } else { throw new Exception("No movie to load with this Id"); } } else { throw new Exception("Movie Id not Valid"); } } } catch (Exception ex) { throw ex; } }
public void InsertTest() { tblMovie newrow = new tblMovie(); // Set the column values newrow.Id = -99; newrow.Title = "My new Movie"; newrow.Description = "Description"; newrow.ImagePath = "ImagePath"; newrow.Cost = 100; newrow.InStockQty = 100; newrow.RatingId = 1; newrow.FormatId = 1; newrow.DirectorId = 1; // Insert of the row dc.tblMovies.Add(newrow); // Commit the changes (insert a row) and then return the rows affected. int rowsaffected = dc.SaveChanges(); Assert.AreNotEqual(0, rowsaffected); }
public static int Update(Movie movie) { using (DVDCentralEntities dc = new DVDCentralEntities()) { try { tblMovie updatedrow = dc.tblMovies.Where(dt => dt.Id == movie.Id).FirstOrDefault(); // Check to see if object exists if (movie != null) { // Update the proper fields updatedrow.Title = movie.Title; updatedrow.Description = movie.Description; updatedrow.ImagePath = movie.ImagePath; updatedrow.Cost = movie.Cost; updatedrow.InStockQty = movie.InStockQty; updatedrow.RatingId = movie.RatingId; updatedrow.FormatId = movie.FormatId; updatedrow.DirectorId = movie.DirectorId; // Save and commit changes return(dc.SaveChanges()); } else { // throw an exception stating the row was not found throw new Exception("Row was not found."); } } catch (Exception ex) { throw ex; } } }
public static bool Insert(Movie movie) { try { using (DVDCentralEntities dc = new DVDCentralEntities()) { // Make a new row tblMovie newrow = new tblMovie(); // Set the properties // Ternary Operator condition ? true : false newrow.Id = dc.tblMovies.Any() ? dc.tblMovies.Max(p => p.Id) + 1 : 1; // If there are any rows, get the max id and add 1, if not use 1 newrow.Title = movie.Title; newrow.Description = movie.Description; newrow.ImagePath = movie.ImagePath; newrow.Cost = movie.Cost; newrow.InStockQty = movie.InStockQty; newrow.RatingId = movie.RatingId; newrow.FormatId = movie.FormatId; newrow.DirectorId = movie.DirectorId; // Do the Insert dc.tblMovies.Add(newrow); // Commit the insert dc.SaveChanges(); return(true); } } catch (Exception ex) { throw ex; } }
public int Delete() { try { using (DVDEntities dc = new DVDEntities()) { //Make sure that the ID is set and valid if (this.Id >= 0) { tblMovie movie = dc.tblMovies.FirstOrDefault(m => m.Id == this.Id); //Make sure that a movie was retrieved if (movie != null) { //Update the proterties on the movie dc.tblMovies.Remove(movie); //return the number of rows effected return(dc.SaveChanges()); } else { throw new Exception("No movie to load with this Id"); } } else { throw new Exception("Movie Id not Valid"); } } } catch (Exception ex) { throw ex; } }
private void button3_Click(object sender, RoutedEventArgs e) { try { tblMovie Movie = new tblMovie(); if (string.IsNullOrWhiteSpace(tbFilmName.Text)) { throw new Exception("Не введено название фильма"); } Movie.MovieName = tbFilmName.Text; if (string.IsNullOrWhiteSpace(tbDirector.Text)) { throw new Exception("Не введено имя режиссера"); } Movie.MovieDirector = tbDirector.Text; Movie.MovieDuration = Convert.ToInt32(udDuration.Value); Movie.MovieYear = Convert.ToInt32(udYear.Value); Movie.MinAge = Convert.ToInt32(udMinAge.Value); if (string.IsNullOrWhiteSpace(tbMainHero.Text)) { throw new Exception("Не введено имя главного актера"); } Movie.MainActor = tbMainHero.Text; if (string.IsNullOrWhiteSpace(tbGenres.Text)) { throw new Exception("Не выбран хотя бы один жанр"); } if (string.IsNullOrWhiteSpace(tbCountries.Text)) { throw new Exception("Не выбран хотя бы одна страна"); } CinemaDataContext Context = new CinemaDataContext(); Context.tblMovies.InsertOnSubmit(Movie); Context.SubmitChanges(); int MovieID = (from M in Context.tblMovies where M.MovieName == tbFilmName.Text select M.MovieID).ToArray()[0]; List <int> GenresID; List <int> CountriesID; GetID(tbGenres.Text, tbCountries.Text, out GenresID, out CountriesID); foreach (int ID in GenresID) { tblMovieGenre MovieGenre = new tblMovieGenre(); MovieGenre.MovieID = MovieID; MovieGenre.GenreID = ID; Context.tblMovieGenres.InsertOnSubmit(MovieGenre); } foreach (int ID in CountriesID) { tblMovieCountry MovieCountry = new tblMovieCountry(); MovieCountry.MovieID = MovieID; MovieCountry.CountryID = ID; Context.tblMovieCountries.InsertOnSubmit(MovieCountry); } Context.SubmitChanges(); ReloadFilmsRooms(); MessageBox.Show("Фильм " + Movie.MovieName + " успешно добавлен"); } catch (Exception Ex) { MessageBox.Show("Ошибка: " + Ex.Message); } }