// Loads the users movie database back into the ListBox public void RetrieveMovies() { string sql = "SELECT MovieTitle, DateReleased, Rated, Genre, Runtime, Plot FROM Watchlater;"; MySqlCommand cmd = new MySqlCommand(sql, conn); MySqlDataReader rdr = cmd.ExecuteReader(); movies = new List <movieData>(); while (rdr.Read()) { newMovie = new movieData(); newMovie.MovieTitle = rdr["MovieTitle"].ToString(); string strDate = rdr["DateReleased"].ToString(); DateTime date = Convert.ToDateTime(strDate); newMovie.ReleaseDate = date; newMovie.Rated = rdr["Rated"].ToString(); newMovie.Genre = rdr["Genre"].ToString(); newMovie.Runtime = rdr["Runtime"].ToString(); newMovie.Plot = rdr["Plot"].ToString(); movies.Add(newMovie); } rdr.Close(); }
// removes the movie object from the listbox private void btnDelete_Click(object sender, EventArgs e) { movieData title = (movieData)listBox1.SelectedItems[0]; sender = title; DeleteMovie?.Invoke(sender, new EventArgs()); listBox1.Items.Remove(listBox1.SelectedItems[0]); }
// Handles the event to display the movie that is double clicked public void HandleDisplayMovie(object sender, EventArgs e) { movieData selectedMovie = (movieData)sender; txtTitle.Text = selectedMovie.MovieTitle; dateReleased.Text = selectedMovie.ReleaseDate.ToShortDateString(); txtRating.Text = selectedMovie.Rated; txtGenre.Text = selectedMovie.Genre; txtRuntime.Text = selectedMovie.Runtime; txtPlot.Text = selectedMovie.Plot; }
// Updates the database and removes the object public void HandleDelete(object sender, EventArgs e) { movieData deleteMovie = (movieData)sender; // application must be able to pass information to and from a database string sql = "DELETE FROM Watchlater WHERE MovieTitle = @MovieTitle;"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd = new MySqlCommand(sql, conn); cmd.Parameters.AddWithValue("@MovieTitle", deleteMovie.MovieTitle); MySqlDataReader rdr = cmd.ExecuteReader(); rdr.Close(); }
// Adds the newMovie object to the SQL database and to the ListBox private void btnAdd_Click(object sender, EventArgs e) { if (list == null) { list = new ListView(); } newMovie = new movieData(); newMovie.MovieTitle = txtTitle.Text; DateTime date = Convert.ToDateTime(dateReleased.Text); newMovie.ReleaseDate = date; newMovie.Rated = txtRating.Text; newMovie.Genre = txtGenre.Text; newMovie.Runtime = txtRuntime.Text; newMovie.Plot = txtPlot.Text; sender = newMovie; if (AddToList != null) { AddToList(sender, new EventArgs()); } // application must be able to pass information to and from a database string sql = "INSERT INTO Watchlater (MovieTitle, DateReleased, Rated, Genre, Runtime, Plot) VALUES (@MovieTitle, @DateReleased, @Rated, @Genre, @Runtime, @Plot);"; MySqlCommand cmd = new MySqlCommand(sql, conn); cmd = new MySqlCommand(sql, conn); cmd.Parameters.AddWithValue("@MovieTitle", newMovie.MovieTitle); cmd.Parameters.AddWithValue("@DateReleased", newMovie.ReleaseDate.ToShortDateString()); cmd.Parameters.AddWithValue("@Rated", newMovie.Rated); cmd.Parameters.AddWithValue("@Genre", newMovie.Genre); cmd.Parameters.AddWithValue("@Runtime", newMovie.Runtime); cmd.Parameters.AddWithValue("@Plot", newMovie.Plot); MySqlDataReader rdr = cmd.ExecuteReader(); rdr.Close(); label6.Text = "Movie Added!"; }
// Handles the event handler for adding a new movie to the listbox public void HandleAddToList(object sender, EventArgs e) { movieData movie = (movieData)sender; listBox1.Items.Add(movie); }