protected void MovieDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            try
            {
                MovieCatalogBL contextBL = new MovieCatalogBL();
                var movieToUpdate = contextBL.GetMovieByID(movieID);

                string contentProvider = null;
                if (e.NewValues["ContentProvider"] == null || e.NewValues["ContentProvider"].ToString() == "")
                {
                    contentProvider = "";
                }
                else
                {
                    contentProvider = e.NewValues["ContentProvider"].ToString();
                }

                string title = e.NewValues["OriginalName"].ToString();
                string genre = e.NewValues["Genre"].ToString();

                //msdn.microsoft.com/en-us/library/system.timespan%28v=vs.110%29.aspx
                //string value = "12:12:15";
                //TimeSpan ts = TimeSpan.Parse(value);
                TimeSpan movieDuration = TimeSpan.Zero;

                // find DropDownList control inside DetailsView
                DropDownList ddlHours = (DropDownList)MovieDetailsView.FindControl("ddlHours");
                DropDownList ddlMinutes = (DropDownList)MovieDetailsView.FindControl("ddlMinutes");
                DropDownList ddlSeconds = (DropDownList)MovieDetailsView.FindControl("ddlSeconds");

                string hours = ddlHours.SelectedValue.ToString().Trim();
                string minutes = ddlMinutes.SelectedValue.ToString().Trim();
                string seconds = ddlSeconds.SelectedValue.ToString().Trim();

                //string value = "12:12:15";
                //TimeSpan ts = TimeSpan.Parse(value);
                string duration1 = hours + ":" + minutes + ":" + seconds;
                movieDuration = TimeSpan.Parse(duration1);


                string country = e.NewValues["Country"].ToString();

                string rightsIPTV = null;
                if (e.NewValues["RightsIPTV"] == null || e.NewValues["RightsIPTV"].ToString() == "")
                {
                    rightsIPTV = string.Empty;
                }
                else
                {
                    rightsIPTV = e.NewValues["RightsIPTV"].ToString();
                }

                string rightsVOD = null;
                if (e.NewValues["RightsVOD"] == null || e.NewValues["RightsVOD"].ToString() == "")
                {
                    rightsVOD = string.Empty;
                }
                else
                {
                    rightsVOD = e.NewValues["RightsVOD"].ToString();
                }

                string svodRights = null;
                if (e.NewValues["SVODRights"] == null || e.NewValues["SVODRights"].ToString() == "")
                {
                    svodRights = string.Empty;
                }
                else
                {
                    svodRights = e.NewValues["SVODRights"].ToString();
                }

                string ancillaryRights = null;
                if (e.NewValues["AncillaryRights"] == null || e.NewValues["AncillaryRights"].ToString() == "")
                {
                    ancillaryRights = string.Empty;
                }
                else
                {
                    ancillaryRights = e.NewValues["AncillaryRights"].ToString();
                }


                DateTime startDate = Convert.ToDateTime(e.NewValues["StartDate"]);
                DateTime expireDate = Convert.ToDateTime(e.NewValues["ExpireDate"]);
                int result = DateTime.Compare(startDate, expireDate);
                string relationship = "Expiry Date is earlier than Start Date !";
                //msdn.microsoft.com/en-us/library/5ata5aya%28v=vs.110%29.aspx
                // if startDate is later than expireDate result is (1) greater than 0 (zero)
                if (result > 0)
                {
                    // instead of "e.Cancel = true;" can be -> ((DetailsViewInsertEventArgs)e).Cancel = true;
                    // www.noordam.it/validating-detailsview-field-NewValues-during-insert-and-update/
                    // has to be 'return' after 'e.Cancel = true;'  -> at least in some cases
                    e.Cancel = true;
                    ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + relationship + "');", true);
                    return;
                }


                string comment = null;
                if (e.NewValues["Comment"] == null || e.NewValues["Comment"].ToString() == "")
                {
                    comment = string.Empty;
                }
                else
                {
                    comment = e.NewValues["Comment"].ToString();
                }

                Int16 year = Convert.ToInt16(e.NewValues["Year"]);

                contextBL.UpdateMovieByID(movieID, contentProvider, title, genre, movieDuration, country, rightsIPTV, rightsVOD, svodRights, ancillaryRights, startDate, expireDate, comment, year);
                lblMessage.ForeColor = System.Drawing.Color.Black;
                lblMessage.Text = "Movie " + title + " updated.";
             }
            catch (DbUpdateException)
            {
                lblMessage.Text = "Update Exception. Error while updating movie data. Please try again.";
            } 
            catch(NullReferenceException)
            {
                lblMessage.Text = "An error occurred while updating movie. Make sure that movie exists.";
            }
            catch(ArgumentNullException)
            {
                lblMessage.Text = "ArgumentNullException: An error occurred while updating movie. Make sure that movie exists.";
            }
            catch (Exception)
            {
                lblMessage.Text = "An error occurred while updating movie. Please try again.";
            }
                        
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            // check QueryString not null OR empty - if null OR empty redirect to Previous page
            if (Request.QueryString["Id"] == null || Request.QueryString["Id"] == "")
            {
                Response.Redirect("Default.aspx");
            }

            movieID = Convert.ToInt32(Request.QueryString["Id"]);
            
            if (!IsPostBack)
            {
                MovieCatalogBL contextBL = new MovieCatalogBL();

                try
                {   
                    var queryMovieByID2 = contextBL.GetMovieByID(movieID);
                    // needs to be List
                    MovieDetailsView.DataSource = queryMovieByID2.ToList();
                    MovieDetailsView.DataBind();
                                                         
                   // find DropDownList control inside DetailsView
                   DropDownList ddlHours = (DropDownList)MovieDetailsView.FindControl("ddlHours");
                   DropDownList ddlMinutes = (DropDownList)MovieDetailsView.FindControl("ddlMinutes");
                   DropDownList ddlSeconds = (DropDownList)MovieDetailsView.FindControl("ddlSeconds");
                   // Time DropDownList
                   for (int index = 0; index < 24; index++)
                   {
                       ddlHours.Items.Add(index.ToString("00"));
                   }
                   for (int index = 0; index < 60; index++)
                   {

                       ddlMinutes.Items.Add(index.ToString("00"));
                       ddlSeconds.Items.Add(index.ToString("00"));
                   }

                   // for Calendar control -- Year & Month DropDownLists
                   LoadYears();
                   LoadMonths();
             
                   // for movie production (make) year
                   LoadProductionYears();
                   DropDownList ddlProductionYear = (DropDownList)MovieDetailsView.FindControl("DropDownListProductionYear");
                   // set year in DropDownListProductionYear
                   ddlProductionYear.Text = queryMovieByID2.FirstOrDefault().Year.ToString();
                   
                   // get Duration of movie
                   TimeSpan duration = queryMovieByID2.FirstOrDefault().Duration.Value;
                   // set the text of ddl's for hours, minutes and seconds
                   ddlHours.Text = duration.Hours.ToString("00");
                   ddlMinutes.Text = duration.Minutes.ToString("00");
                   ddlSeconds.Text = duration.Seconds.ToString("00");

                   // Fill CheckBoxLists with countries
                   CheckBoxList ddlCountry = (CheckBoxList)MovieDetailsView.FindControl("ddlCountry");
                   ddlCountry.DataSource = CountriesList();
                   ddlCountry.DataBind();
                    
                   CheckBoxList ddlCountriesIPTV = (CheckBoxList)MovieDetailsView.FindControl("ddlCountriesIPTV");
                   ddlCountriesIPTV.DataSource = CountriesList();
                   ddlCountriesIPTV.DataBind();

                   CheckBoxList ddlCountriesVOD = (CheckBoxList)MovieDetailsView.FindControl("ddlCountriesVOD");
                   ddlCountriesVOD.DataSource = CountriesList();
                   ddlCountriesVOD.DataBind();
                    

                   // select countries in ddlCountry
                   string countriesMovie = queryMovieByID2.FirstOrDefault().Country;
                   //Convert the string into an array of words 
                   string[] countryArray = countriesMovie.Split(new char[] { '?', '!', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
                                                        
                   // has to be 'ListItem' "(ListItem item in ddlCountry.Items)" -> NOT "var"(var item in ddlCountries.Items)
                   foreach (ListItem item in ddlCountry.Items)
                   {
                       for (int k = 0; k < countryArray.Length; k++)
                       {
                           if (item.ToString().ToUpperInvariant() == countryArray[k].ToUpperInvariant().Trim())
                           {
                               item.Selected = true;
                           }
                       }
                   }

                   // select countries in ddlIPTV
                    string countriesIPTV = queryMovieByID2.FirstOrDefault().RightsIPTV;
                   //Convert the string into an array of words 
                   string[] iptvArray = countriesIPTV.Split(new char[] { '?', '!', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
                   // has to be 'ListItem' "(ListItem item in ddlcountriesIPTV.Items)" -> NOT "var"(var item in ddlCountries.Items)
                   foreach (ListItem item in ddlCountriesIPTV.Items)
                   {
                       for (int k = 0; k < iptvArray.Length; k++)
                       {
                           if (item.ToString().ToUpperInvariant() == iptvArray[k].ToUpperInvariant().Trim())
                           {
                               item.Selected = true;
                           }
                       }
                   }


                   // select countries in ddlVOD
                    string countriesVOD = queryMovieByID2.FirstOrDefault().RightsVOD;
                   //Convert the string into an array of words 
                    string[] VODArray = countriesVOD.Split(new char[] { '?', '!', ';', ':', ',' }, StringSplitOptions.RemoveEmptyEntries);
                   // has to be 'ListItem' "(ListItem item in ddlCountriesVOD.Items)" -> NOT "var"(var item in ddlCountriesVOD.Items)
                   foreach (ListItem item in ddlCountriesVOD.Items)
                   {
                       for (int k = 0; k < VODArray.Length; k++)
                       {
                           if (item.ToString().ToUpperInvariant() == VODArray[k].ToUpperInvariant().Trim())
                           {
                               item.Selected = true;
                           }
                       }
                   }

                   // set text/value in DropdDownCheckBox Controls: ddlAncillaryRights and ddlSVODRights
                   string svodRights = queryMovieByID2.FirstOrDefault().SVODRights.ToString();
                   string ancillaryRights = queryMovieByID2.FirstOrDefault().AncillaryRights.ToString();
                   DropDownList ddlAncillaryRights = (DropDownList)MovieDetailsView.FindControl("ddlAncillaryRights");
                   DropDownList ddlSVODRights = (DropDownList)MovieDetailsView.FindControl("ddlSVODRights");
                   ddlAncillaryRights.Text = ancillaryRights;
                   ddlSVODRights.Text = svodRights;

                   // set the date in startDateCalendar and make it visible in control
                   DateTime startDate = (DateTime)queryMovieByID2.First().StartDate;
                   System.Web.UI.WebControls.Calendar startDateCalendar = (System.Web.UI.WebControls.Calendar)MovieDetailsView.FindControl("startDateCalendar");
                   startDateCalendar.SelectedDate = startDate;
                   startDateCalendar.VisibleDate = startDate;
                   // set the Year and Month in ddlStartYear and ddlStartMonth
                   DropDownList ddlStartYear = (DropDownList)MovieDetailsView.FindControl("DropDownListYear");
                   DropDownList ddlStartMonth = (DropDownList)MovieDetailsView.FindControl("DropDownListMonth");
                   ddlStartYear.Text = startDate.Year.ToString();
                   ddlStartMonth.Text = startDate.Month.ToString();

                   
                    
                   // set the date in expiryDateCalendar and make it visible in control
                   DateTime expiryDate = (DateTime)queryMovieByID2.First().ExpireDate;
                   System.Web.UI.WebControls.Calendar expiryDateCalendar = (System.Web.UI.WebControls.Calendar)MovieDetailsView.FindControl("expireDateCalendar");
                   expiryDateCalendar.SelectedDate = expiryDate;
                   expiryDateCalendar.VisibleDate = expiryDate;
                   // set the Year and Month in ddlExpiryYear and ddlExpiryMonth
                    DropDownList ddlExpiryYear = (DropDownList)MovieDetailsView.FindControl("DropDownListExpireYear");
                   DropDownList ddlExpiryMonth = (DropDownList)MovieDetailsView.FindControl("DropDownListExpireMonth");
                   ddlExpiryYear.Text = expiryDate.Year.ToString();
                   ddlExpiryMonth.Text = expiryDate.Month.ToString();
                }
                catch (Exception)
                {
                    lblMessage.Text = "An error occurred. Please try again.";
                }
                                                               
            }
        }