Exemplo n.º 1
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();
                }
            }
        }
Exemplo n.º 2
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();
                }
            }
        }