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"); } } }
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"); } } }
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"); } } }
protected void Page_PreRender(object sender, EventArgs e) { if (this.IsPostBack) { return; } var dbContext = new EarthDbEntities(); using (dbContext) { var countries = dbContext.Countries.OrderBy(town => town.Name).ToList(); this.DropDownListCountries.DataSource = countries; this.DataBind(); } }
protected void Page_Prerender(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) { this.TextBoxCountryName.Text = country.Name; this.TextBoxCountryLanguage.Text = country.Language; this.TextBoxCountryPopulation.Text = country.Population.ToString(); } } }
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"); } }