예제 #1
0
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to update '" + txtTitle.Text + "'s information?", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            if (res == DialogResult.OK)
            {
                try
                {
                    Movie         currMovie = movieList[moviesBox.SelectedIndex];
                    SqlConnection conn      = DatabaseManager.GetConnection();
                    StringBuilder sb        = new StringBuilder("Update Movies set ");
                    bool          changed   = txtDirector.Text != currMovie.Director || txtTitle.Text != currMovie.Title ||
                                              txtYear.Text != currMovie.Year.ToString() || txtScore.Text != currMovie.RottenTomatoesScore.ToString() ||
                                              txtEarnings.Text != currMovie.TotalBoxOffice.ToString() || cboxGenre.SelectedIndex != currMovie.IndexGenre;
                    if (!changed)
                    {
                        throw new Exception("The movie's information remains the same, please change a field to update it in the database!");
                    }
                    if (txtDirector.Text != currMovie.Director)
                    {
                        sb.Append($"Director = '{txtDirector.Text}'");
                    }
                    if (txtTitle.Text != currMovie.Title)
                    {
                        sb.Append($"Title = '{txtTitle.Text}'");
                    }
                    if (txtYear.Text != currMovie.Year.ToString())
                    {
                        sb.Append($"Year = {txtYear.Text}");
                    }
                    if (txtScore.Text != currMovie.RottenTomatoesScore.ToString())
                    {
                        sb.Append($"RottenTomatoesScore = {txtScore.Text}");
                    }
                    if (txtEarnings.Text != currMovie.TotalBoxOffice.ToString())
                    {
                        sb.Append($"TotalEarned = {Convert.ToDecimal(txtEarnings.Text)}");
                    }
                    if (cboxGenre.SelectedIndex != currMovie.IndexGenre)
                    {
                        sb.Append($"Genre = {cboxGenre.SelectedIndex}");
                    }

                    sb.Append($" where Id = {currMovie.Id};");
                    SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
                    if (cmd.ExecuteNonQuery() < 1)
                    {
                        throw new Exception("Movie not found, please refresh this page");
                    }
                    MessageBox.Show("Movie succesfully updated!");
                    conn.Close();
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
            }
            if (res == DialogResult.Cancel)
            {
                txtTitle.Focus();
            }
        }