public ActionResult Create(FormCollection collection) { try { var movie = new Movie { Id = _movies.Count + 1, Title = collection["Title"], Genre = collection["Genre"] }; using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("Insert into movies (Title, Genre) Values ('" + movie.Title + "','" + movie.Genre + "');"); cmd.Connection = conn; cmd.ExecuteNonQuery(); } _movies.Add(movie); return RedirectToAction("Index"); } catch { return View(); } }
public Movie ReturnDetails(int id) { var movie = new Movie(); using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("Select id, title, genre from movies where id='" + id + "'"); cmd.Connection = conn; var reader = cmd.ExecuteReader(); while (reader.Read()) { movie.Id = (int)reader.GetValue(0); movie.Title = (string)reader.GetString(1); movie.Genre = (string)reader.GetString(2); } } return movie; }
public ActionResult Edit(int id, FormCollection collection) { var movie = new Movie(); using (var conn = new SqlConnection(ConnectionString)) { try { movie = new Movie(); { movie.Id = id; movie.Title = collection["Title"]; movie.Genre = collection["Genre"]; } conn.Open(); SqlCommand cmd = new SqlCommand("Update Movies set Title='" + movie.Title + "', Genre='" + movie.Genre + "' where id ='" + id + "'"); cmd.Connection = conn; cmd.ExecuteNonQuery(); return RedirectToAction("Index"); } catch { return View(); } } }
// GET: Movie/Edit/5 public ActionResult Edit(int id) { var movie = new Movie(); using (var conn = new SqlConnection(ConnectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("Select Title, Genre from Movies where id ='" + id + "'"); cmd.Connection = conn; var reader = cmd.ExecuteReader(); while (reader.Read()) { movie.Id = id; movie.Title = reader.GetString(0); movie.Genre = reader.GetString(1); } } return View(movie); }