Esempio n. 1
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;
                }
            }
        }
Esempio n. 2
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;
                }
            }
        }
Esempio n. 3
0
        protected void ButtonAddContinent_Click(object sender, EventArgs e)
        {
            string newName = this.TextBoxUpdateContinent.Text;

            if (string.IsNullOrWhiteSpace(newName))
            {
                Response.Write("Please enter name to Create continent!");
                return;
            }

            if (newName.Length > 20)
            {
                Response.Write("Continent name must be less than 20 symbols!");
            }

            WorldEntities db = new WorldEntities();

            Continent newContinent = new Continent()
            {
                ContinentName = newName
            };

            db.Continents.Add(newContinent);
            db.SaveChanges();

            Response.Redirect(Request.RawUrl);
        }
Esempio n. 4
0
        protected void ButtonDeleteContinent_Click(object sender, EventArgs e)
        {
            string newName = Server.HtmlEncode(this.TextBoxUpdateContinent.Text);

            if (string.IsNullOrWhiteSpace(newName))
            {
                Response.Write("Please enter name to delete continent!");
                return;
            }

            var           selectedContinentId = this.ListBoxContinents.SelectedItem.Value;
            WorldEntities db = new WorldEntities();

            var continent = db.Continents.FirstOrDefault(c => c.ContinentId.ToString() == selectedContinentId);

            if (continent == null)
            {
                Response.Write("Selected continent was not found!");
                return;
            }

            db.Entry <Continent>(continent).State = System.Data.Entity.EntityState.Deleted;
            db.SaveChanges();

            Response.Redirect(Request.RawUrl);
        }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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();
                }
            }
        }
Esempio n. 7
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();
                }
            }
        }
Esempio n. 8
0
        protected void ButtonAddCountry_Click(object sender, EventArgs e)
        {
            string newCountryID = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddCountryId");

            if (!IsValidCountryId(newCountryID))
            {
                return;
            }

            string newCountryName = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddCountry");

            if (!IsValidCountryName(newCountryName))
            {
                return;
            }

            string newCountryLatitude = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddLatitude");
            float  latitude;

            if (!float.TryParse(newCountryLatitude, out latitude))
            {
                Response.Write("Enter correct latitude!");
                return;
            }

            string newCountryLongitude = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddLongitude");
            float  longitude;

            if (!float.TryParse(newCountryLongitude, out longitude))
            {
                Response.Write("Enter correct longitude!");
                return;
            }

            string newCountrySurfaceArea = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddSurfaceArea");
            int    surfaceArea;

            if (!int.TryParse(newCountrySurfaceArea, out surfaceArea))
            {
                Response.Write("Enter correct Surface Area (int number)!");
                return;
            }

            string newCountryPopulation = this.GetFooterRowTextBoxText(this.GridViewCountries, "TextBoxAddPopulation");
            int    population;

            if (!int.TryParse(newCountryPopulation, out population))
            {
                Response.Write("Enter correct population (int number)!");
                return;
            }

            ListBox listBoxLanguages = this.GridViewCountries.FooterRow.FindControl("ListBoxAddLanguages") as ListBox;

            int[] listBoxLanguagesIndices = listBoxLanguages.GetSelectedIndices();

            if (listBoxLanguagesIndices.Length < 1)
            {
                Response.Write("Correct Country is required!");
                return;
            }

            Country newCountry = new Country()
            {
                CountryId   = newCountryID,
                CountryName = newCountryName,
                Latitude    = latitude,
                Longitude   = longitude,
                SurfaceArea = surfaceArea,
                Population  = population
            };

            WorldEntities db                     = new WorldEntities();
            var           allLanguages           = listBoxLanguages.Items;
            string        currSelectedLanguageId = string.Empty;
            Language      currLanguage;

            foreach (int languageIndex in listBoxLanguagesIndices)
            {
                currSelectedLanguageId = allLanguages[languageIndex].Value;
                currLanguage           = db.Languages.FirstOrDefault(lang => lang.LanguageId.ToString() == currSelectedLanguageId);

                if (currLanguage == null)
                {
                    Response.Write("Invalid Country entered!");
                    return;
                }

                newCountry.Languages.Add(currLanguage);
            }

            var selectedContinentId = this.ListBoxContinents.SelectedItem.Value;

            var continent = db.Continents.FirstOrDefault(c => c.ContinentId.ToString() == selectedContinentId);

            if (continent == null)
            {
                Response.Write("Selected continent was not found!");
                return;
            }

            newCountry.Continent = continent;

            FileUpload fileUploadControl = this.GridViewCountries.FooterRow.FindControl("FileUploadFlagChange") as FileUpload;

            byte[] fileData = this.GetUploadedFile(fileUploadControl.PostedFile);
            newCountry.FlagImage = fileData;

            db.Countries.Add(newCountry);
            db.SaveChanges();

            Response.Redirect(Request.RawUrl);
        }