Пример #1
0
        protected void OnBtnAddCountry_Click(object sender, EventArgs e)
        {
            var dbContext = new EarthDbEntities();

            using (dbContext)
            {
                int continentId = int.Parse(this.Request.Params["continentId"]);
                var continent   = dbContext.Continents.Find(continentId);

                if (continent != null)
                {
                    string countryName       = this.TextBoxCountryName.Text;
                    int    countryPopulation = int.Parse(this.TextBoxCountryPopulation.Text);
                    string countryLanguage   = this.TextBoxCountryLanguage.Text;

                    var newCountry = new Country()
                    {
                        Continent  = continent,
                        Language   = countryLanguage,
                        Name       = countryName,
                        Population = countryPopulation
                    };

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

                    Response.Redirect("AppMain.aspx");
                }
            }
        }
Пример #2
0
        protected void OnBtnAddTown_Click(object sender, EventArgs e)
        {
            var dbContext = new EarthDbEntities();

            using (dbContext)
            {
                int countryId = int.Parse(this.DropDownListCountries.SelectedValue);
                var country   = dbContext.Countries.Find(countryId);

                if (country != null)
                {
                    var newTown = new Town()
                    {
                        Country    = country,
                        Name       = this.TextBoxTownName.Text,
                        Population = this.TextBoxTownPopulation.Text
                    };

                    dbContext.Towns.Add(newTown);
                    dbContext.SaveChanges();

                    this.Response.Redirect("AppMain.aspx");
                }
            }
        }
Пример #3
0
        protected void OnBtnEditCountry_Click(object sender, EventArgs e)
        {
            var context = new EarthDbEntities();

            using (context)
            {
                int countryId = int.Parse(this.Request.Params["countryId"]);
                var country   = context.Countries.Find(countryId);

                if (country != null)
                {
                    string name       = this.TextBoxCountryName.Text;
                    string language   = this.TextBoxCountryLanguage.Text;
                    int    population = int.Parse(this.TextBoxCountryPopulation.Text);

                    country.Name       = name;
                    country.Language   = language;
                    country.Population = population;

                    context.SaveChanges();

                    Response.Redirect("AppMain.aspx");
                }
            }
        }
Пример #4
0
        protected void OnBtnYes_Click(object sender, EventArgs e)
        {
            var dbContext = new EarthDbEntities();

            using (dbContext)
            {
                int countryId = int.Parse(this.Request.Params["countryId"]);
                var country   = dbContext.Countries.Find(countryId);

                if (country != null)
                {
                    dbContext.Countries.Remove(country);
                    dbContext.SaveChanges();
                }

                this.Response.Redirect("AppMain.aspx");
            }
        }