Exemple #1
0
        protected void ButtonUpdateContinent_Click(object sender, EventArgs e)
        {
            this.LabelContinentErrors.Text = string.Empty;

            if (string.IsNullOrWhiteSpace(this.ListBoxContinents.SelectedValue))
            {
                this.LabelContinentErrors.Text = "No continent selected.";
                return;
            }

            string newContinentName = this.TextBoxContinentName.Text.Trim();

            if (string.IsNullOrWhiteSpace(newContinentName))
            {
                this.LabelContinentErrors.Text = "No continent name specified.";
                return;
            }

            using (var context = new WorldEntities())
            {
                int continentId = int.Parse(this.ListBoxContinents.SelectedValue);
                var continent = context.Continents.FirstOrDefault(c => c.ContinentId == continentId);
                if (continent != null)
                {
                    continent.ContinentName = newContinentName;
                    context.Entry<Continent>(continent).State = EntityState.Modified;
                    context.SaveChanges();
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    this.LabelContinentErrors.Text = "No continent found with id=" + continentId;
                }
            }
        }
Exemple #2
0
        protected void ButtonAddContinent_Click(object sender, EventArgs e)
        {
            this.LabelContinentErrors.Text = string.Empty;

            string newContinentName = this.TextBoxNewContinentName.Text.Trim();

            if (string.IsNullOrWhiteSpace(newContinentName))
            {
                this.LabelContinentErrors.Text = "No continent name specified.";
                return;
            }

            using (var context = new WorldEntities())
            {
                context.Continents.Add(new Continent
                {
                    ContinentName = newContinentName
                });

                context.SaveChanges();
                Response.Redirect(Request.RawUrl);
            }
        }
Exemple #3
0
        protected void ButtonDeleteContinent_Click(object sender, EventArgs e)
        {
            this.LabelContinentErrors.Text = string.Empty;

            if (string.IsNullOrWhiteSpace(this.ListBoxContinents.SelectedValue))
            {
                this.LabelContinentErrors.Text = "No continent selected.";
                return;
            }

            string confirmValue = Request.Form["confirm-value"];
            if (confirmValue == "No")
            {
                return;
            }

            using (var context = new WorldEntities())
            {
                int continentId = int.Parse(this.ListBoxContinents.SelectedValue);
                var continent = context.Continents.FirstOrDefault(c => c.ContinentId == continentId);
                if (continent != null)
                {
                    context.Continents.Remove(continent);
                    context.SaveChanges();
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    this.LabelContinentErrors.Text = "No continent found with id=" + continentId;
                }
            }
        }
Exemple #4
0
        protected void ButtonAddCountry_Click(object sender, EventArgs e)
        {
            WorldEntities context = null;
            this.LabelCountryErrors.Text = string.Empty;

            try
            {
                string countryId = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewCountryId");
                this.ValidateCountryId(countryId);

                string countryName = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewCountryName");
                this.ValidateName(countryName);

                float latitude;
                string latitudeAsString = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewLatitude");
                this.ValidateGeoCoordinate(latitudeAsString, out latitude);

                float longitude;
                string longitudeAsString = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewLongitude");
                this.ValidateGeoCoordinate(longitudeAsString, out longitude);

                float area;
                string areaAsString = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewSurfaceArea");
                this.ValidateArea(areaAsString, out area);

                int population;
                string populationAsString = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewPopulation");
                this.ValidatePopulation(populationAsString, out population);

                context = new WorldEntities();

                int continentId = int.Parse(this.ListBoxContinents.SelectedValue);

                FileUpload flagImageUpload = this.GetFooterRowControl(this.GridViewCountries, "FileUploadNewCountryFlag") as FileUpload;
                byte[] fileData = this.GetUploadedFile(flagImageUpload.PostedFile);

                var country = new Country
                {
                    CountryId = countryId,
                    CountryName = countryName,
                    Latitude = latitude,
                    Longitude = longitude,
                    SurfaceArea = area,
                    ContinentId = continentId,
                    Population = population,
                    FlagImage = fileData
                };

                string newLanguageNames = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxNewLanguages");
                string[] languageNames = newLanguageNames.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string languageName in languageNames)
                {
                    var language = context.Languages.FirstOrDefault(l => string.Compare(l.LanguageName, languageName, true) == 0);
                    if (language == null)
                    {
                        language = new Language
                        {
                            LanguageName = languageName
                        };

                        context.Languages.Add(language);
                        context.SaveChanges();
                    }

                    country.Languages.Add(language);
                }

                context.Countries.Add(country);

                context.SaveChanges();
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                this.LabelCountryErrors.Text = ex.Message;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }
Exemple #5
0
        protected void GridViewCountries_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            WorldEntities context = null;
            this.LabelCountryErrors.Text = string.Empty;

            try
            {
                string countryName = (this.GridViewCountries.Rows[e.RowIndex].FindControl("TextBoxCountryName") as TextBox).Text;
                this.ValidateName(countryName);

                float latitude;
                string latitudeAsString = (this.GridViewCountries.Rows[e.RowIndex].FindControl("TextBoxLatitude") as TextBox).Text;
                this.ValidateGeoCoordinate(latitudeAsString, out latitude);

                float longitude;
                string longitudeAsString = (this.GridViewCountries.Rows[e.RowIndex].FindControl("TextBoxLongitude") as TextBox).Text;
                this.ValidateGeoCoordinate(longitudeAsString, out longitude);

                float area;
                string areaAsString = (this.GridViewCountries.Rows[e.RowIndex].FindControl("TextBoxSurfaceArea") as TextBox).Text;
                this.ValidateArea(areaAsString, out area);

                int population;
                string populationAsString = (this.GridViewCountries.Rows[e.RowIndex].FindControl("TextBoxPopulation") as TextBox).Text;
                this.ValidatePopulation(populationAsString, out population);

                context = new WorldEntities();

                string selectedCountryId = this.GridViewCountries.DataKeys[e.RowIndex].Value.ToString();

                var country = context.Countries.FirstOrDefault(c => c.CountryId == selectedCountryId);
                if (country != null)
                {
                    var postedFile = (this.GridViewCountries.Rows[e.RowIndex].FindControl("FileUploadChangeFlag") as FileUpload).PostedFile;

                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        byte[] data = this.GetUploadedFile(postedFile);

                        country.FlagImage = data;

                        context.Entry<Country>(country).State = EntityState.Modified;

                        context.SaveChanges();
                    }

                    country.Languages.Clear();

                    string newLanguageNames = this.GetTextBoxText(this.GridViewCountries, e.RowIndex, "TextBoxLanguages");
                    string[] languageNames = newLanguageNames.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string languageName in languageNames)
                    {
                        var language = context.Languages.FirstOrDefault(l => string.Compare(l.LanguageName, languageName, true) == 0);
                        if (language == null)
                        {
                            language = new Language
                            {
                                LanguageName = languageName
                            };

                            context.Languages.Add(language);
                            context.SaveChanges();
                        }

                        country.Languages.Add(language);
                    }

                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                this.LabelCountryErrors.Text = ex.Message;
                e.Cancel = true;
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                }
            }
        }