protected void DeleteContintent_Click(object sender, EventArgs e) { using (var context = new Data.WorldEntities()) { if (this.SelectedContinent.SelectedItem == null) { this.ErrorMessageHolder.InnerText = "No continent selected"; return; } var selectedId = int.Parse(this.SelectedContinent.SelectedItem.Value); var continent = context.Continents.FirstOrDefault(c => c.Id == selectedId); if (continent == null) { this.ErrorMessageHolder.InnerText = "No such continent found. Try again."; return; } var countries = context.Countries.Include("Towns").Where(c => c.ContinentId == selectedId); foreach (var country in countries) { context.Towns.RemoveRange(country.Towns); context.Countries.Remove(country); } context.Continents.Remove(continent); context.SaveChanges(); Server.Transfer("EditContinents.aspx"); } }
protected void EditContinent_Click(object sender, EventArgs e) { Page.Validate("EditContinent"); if (this.SelectedContinent.SelectedItem == null) { this.ErrorMessageHolder.InnerText = "No continent selected"; return; } var id = int.Parse(this.SelectedContinent.SelectedValue); var newName = this.EditContinentName.Text; if (newName.Length < 4 || newName.Length > 50) { this.ErrorMessageHolder.InnerText = "Continent's name must be at least 4 symbols and not more then 50 symbols"; return; } using (var context = new Data.WorldEntities()) { var continent = context.Continents.Find(id); if (continent == null) { this.ErrorMessageHolder.InnerText = "No such continent found. Try again."; return; } continent.Name = newName; context.SaveChanges(); Server.Transfer("EditContinents.aspx"); } }
protected void Delete_Command(object sender, CommandEventArgs e) { var languageId = Convert.ToInt32(e.CommandArgument); var countryId = Convert.ToInt32(Request.Params["countryId"]); var context = new Data.WorldEntities(); var country = context.Countries.Include("Languages").FirstOrDefault(c => c.Id == countryId); if (country == null) { this.ErrorMessage.Text = "Wrong Country selected!"; return; } var language = country.Languages.FirstOrDefault(l => l.Id == languageId); if (language == null) { this.ErrorMessage.Text = "Wrong Language selected!"; return; } country.Languages.Remove(language); try { context.SaveChanges(); } catch (Exception ex) { this.ErrorMessage.Text = ex.Message; } }
protected void DeleteSelectedLanguage_Click(object sender, EventArgs e) { var languageIndeces = this.SelectLanguageListBox.GetSelectedIndices(); var languageIds = new List <int>(); foreach (var index in languageIndeces) { var id = this.SelectLanguageListBox.Items[index].Value; languageIds.Add(Convert.ToInt32(id)); } var context = new Data.WorldEntities(); foreach (var id in languageIds) { var language = context.Languages.Find(id); context.Languages.Remove(language); } try { context.SaveChanges(); this.SelectLanguageListBox.DataBind(); } catch (Exception ex) { this.ErrorMessage.Text = ex.Message; } }
private void SaveCountryIfChanged(Country country) { var cachedCopy = this.copy.Countries.FirstOrDefault(c => c.Id == country.Id); if (cachedCopy == null) { using (var context = new Data.WorldEntities()) { context.Countries.Add(new Country { Name = country.Name, Population = country.Population }); } } if (cachedCopy.Name != country.Name || cachedCopy.Population != country.Population) { using (var context = new Data.WorldEntities()) { var countryInDb = context.Countries.Find(country.Id); countryInDb.Name = country.Name; countryInDb.Population = country.Population; context.SaveChanges(); } } }
private void SaveTownIfChanged(Town town) { var cachedCopy = this.copy.Towns.FirstOrDefault(t => t.Id == town.Id); if (cachedCopy == null) { using (var context = new Data.WorldEntities()) { context.Towns.Add(new Town { Name = town.Name }); } } if (cachedCopy.Name != town.Name) { using (var context = new Data.WorldEntities()) { var townInDb = context.Towns.Find(town.Id); townInDb.Name = town.Name; context.SaveChanges(); } } }
protected void ChangeFlagButton_Command(object sender, CommandEventArgs e) { var countryId = Convert.ToInt32(e.CommandArgument); var file = this.CountriesListView.EditItem.Controls[this.CountriesListView.EditItem.Controls.Count - 2] as FileUpload; //var fileOld = this.UploadFile; if (file == null || file.FileBytes.Length == 0) { ShowErrorMessage("Select file to upload"); return; } if (file.FileName.IndexOf(".png") <= 0) { ShowErrorMessage("File must be a .PNG image"); return; } var image = ImageHelper.ResizeImage(32, 32, file.FileBytes); var context = new Data.WorldEntities(); var country = context.Countries.Find(countryId); country.Flag = image; context.SaveChanges(); this.CountriesListView.UpdateItem(this.CountriesListView.EditItem.DisplayIndex, false); }
protected void Page_PreRender(object sender, EventArgs e) { var context = new Data.WorldEntities(); var countryId = Convert.ToInt32(Request.Params["countryId"]); var selectedCountry = context.Countries.Include("Languages") .FirstOrDefault(c => c.Id == countryId); this.CountrySelected.Text = selectedCountry.Name; this.CurrentLanguages.DataSource = selectedCountry.Languages; this.CurrentLanguages.DataBind(); }
protected void AddSelectedLanguages_Click(object sender, EventArgs e) { var selectedIndices = this.SelectLanguageListBox.GetSelectedIndices(); var languagesIds = new List <int>(); foreach (var index in selectedIndices) { var id = int.Parse(this.SelectLanguageListBox.Items[index].Value); languagesIds.Add(id); } var context = new Data.WorldEntities(); var country = context.Countries.Find(Convert.ToInt32(Request.Params["countryId"])); if (country == null) { this.ErrorMessage.Text = "No such country exists!"; return; } foreach (var id in languagesIds) { var lanuguage = context.Languages.Find(id); if (lanuguage == null) { this.ErrorMessage.Text = "Error in language selection!"; return; } country.Languages.Add(lanuguage); } try { context.SaveChanges(); } catch (Exception ex) { this.ErrorMessage.Text = ex.Message; } }
protected void AddNewContinent_Click(object sender, EventArgs e) { Page.Validate("InsertContinent"); var newName = this.NewContinentName.Text; if (newName.Length < 4 || newName.Length > 50) { this.ErrorMessageHolder.InnerText = "Continent's name must be at least 4 symbols and not more then 50 symbols"; return; } using (var context = new Data.WorldEntities()) { context.Continents.Add(new Data.Continent { Name = newName }); context.SaveChanges(); Server.Transfer("EditContinents.aspx"); } }
protected void AddNewLanguageButton_Click(object sender, EventArgs e) { var newLanguageName = this.AddNewLanguageTextBox.Text; var context = new Data.WorldEntities(); context.Languages.Add(new Language { Name = newLanguageName }); try { context.SaveChanges(); this.SelectLanguageListBox.DataBind(); } catch (Exception ex) { this.ErrorMessage.Text = ex.Message; } }
protected void CountriesDataSource_Deleting(object sender, EntityDataSourceChangingEventArgs e) { var editedCountry = (Data.Country)e.Entity; using (var context = new Data.WorldEntities()) { var editedCountryInDB = context.Countries .Include("Towns").FirstOrDefault(c => c.Id == editedCountry.Id); if (editedCountryInDB == null) { return; } context.Towns.RemoveRange(editedCountryInDB.Towns); editedCountryInDB.Languages.Clear(); context.SaveChanges(); } }
// The id parameter name should match the DataKeyNames value set on the control public void CountriesGrid_DeleteItem(int id) { using (var context = new Data.WorldEntities()) { var country = context.Countries.Include("Towns").FirstOrDefault(c => c.Id == id); if (country == null) { return; } var towns = country.Towns; if (towns.Count > 0) { foreach (var town in towns) { context.Towns.Remove(town); } } context.Countries.Remove(country); context.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { var context = new Data.WorldEntities(); }