private void btn_delete_Click(object sender, EventArgs e) { if (grdDistrict.SelectedRows.Count > 0) { var districtViewModel = grdDistrict.SelectedRows[0].DataBoundItem as DistrictViewModel; if (districtViewModel != null) { var dialogResult = MessageBox.Show($@"Do you really want to delete the District {districtViewModel.DistrictName}?", @"Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); if (dialogResult == DialogResult.Yes) { var dbContext = new MyCountryEntities(); var districtCode = districtViewModel.DistrictCode; var district = dbContext.Districts.FirstOrDefault(x => x.DistrictCode == districtCode); if (district != null) { dbContext.Districts.Remove(district); dbContext.SaveChanges(); SearchDistrictInformations(); MessageBox.Show(@"Successfully deleted the district.", @"Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } else { MessageBox.Show(@"Please select a district to delete", @"Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
public void Add(District districtInfo) { using (var dbContext = new MyCountryEntities()) { var existingDistrict = InnerGetDistrictByCode(dbContext, districtInfo.DistrictCode); if (existingDistrict != null) { //raise duplicated district error here throw new DistrictValidationException($"District code {existingDistrict.DistrictCode} exists."); } var district = new District { DistrictCode = districtInfo.DistrictCode, Name = districtInfo.Name, Type = districtInfo.Type, CityCode = districtInfo.CityCode, CreatedDate = DateTime.Now, CreatedBy = ServiceContext.UserName, ModifiedDate = DateTime.Now, ModifiedBy = ServiceContext.UserName }; dbContext.Districts.Add(district); dbContext.SaveChanges(); } }
public void Add(District districtParameter) { using (var dbContext = new MyCountryEntities()) { var currentDateTime = DateTimeUtil.GetCurrentDateTime(); var existingDistrict = InnerGetDistrictByCode(dbContext, districtParameter.DistrictCode); if (existingDistrict != null) { throw new DistrictValiationException($@"District code {existingDistrict.DistrictCode} exists."); } var district = new District { Name = districtParameter.Name, DistrictCode = districtParameter.DistrictCode, Type = districtParameter.Type, CityCode = districtParameter.CityCode, CreatedDate = currentDateTime, CreatedBy = ServiceContext.UserName, ModifiedDate = currentDateTime, ModifiedBy = ServiceContext.UserName }; dbContext.Districts.Add(district); dbContext.SaveChanges(); } }
public void Update(District districtInfo) { using (var dbContext = new MyCountryEntities()) { var existingDistrict = InnerGetDistrictByCode(dbContext, districtInfo.DistrictCode); if (existingDistrict != null) { existingDistrict.Name = districtInfo.Name; existingDistrict.Type = districtInfo.Type; existingDistrict.ModifiedDate = DateTime.Now; existingDistrict.ModifiedBy = ServiceContext.UserName; dbContext.SaveChanges(); } } }
public void Update(District districtParameter) { using (var dbContext = new MyCountryEntities()) { var currentDateTime = DateTimeUtil.GetCurrentDateTime(); var existingDistrict = InnerGetDistrictByCode(dbContext, districtParameter.DistrictCode); if (existingDistrict == null) { throw new DistrictValiationException(@"Cannot found district"); } existingDistrict.Name = districtParameter.Name; existingDistrict.Type = districtParameter.Type; existingDistrict.ModifiedDate = currentDateTime; existingDistrict.ModifiedBy = ServiceContext.UserName; dbContext.SaveChanges(); } }
private void btnSave_Click(object sender, EventArgs e) { try { if (_isAddNew) { var dbContext = new MyCountryEntities(); var city = cbbCity.SelectedItem as City; var district = new District { Name = txtName.Text, DistrictCode = txtCode.Text, Type = txtType.Text, CityCode = city != null ? city.CityCode : string.Empty }; dbContext.Districts.Add(district); dbContext.SaveChanges(); } else { //Todo: Implement editing district var dbContext = new MyCountryEntities(); var district = dbContext.Districts.FirstOrDefault(x => x.DistrictCode == _selectedCode); if (district != null) { district.Name = txtName.Text; district.Type = txtType.Text; dbContext.SaveChanges(); } } } catch (Exception exception) { MessageBox.Show(exception.Message); } }