private void Page_Loaded(object sender, RoutedEventArgs e) { if (!IdTerritory.Equals("")) { txtTerritoryId.IsReadOnly = true; using (var db = new Model.NorthwindDataContext()) { (from t in db.Territories join r in db.Region on t.RegionID equals r.RegionID where t.TerritoryID.Equals(IdTerritory) select new { t.TerritoryID, t.TerritoryDescription, r.RegionDescription }) .ToList() .ForEach(x => { txtTerritoryId.Text = x.TerritoryID; txtDescription.Text = x.TerritoryDescription; cmbRegion.Text = x.RegionDescription; }); } } }
bool IdExist(string id) { using (var db = new Model.NorthwindDataContext()) { return(db.Territories.Any(x => x.TerritoryID.Equals(id))); } }
void UpdateTerritory() { using (var db = new Model.NorthwindDataContext()) { var query = db.Territories.Where(x => x.TerritoryID.Equals(IdTerritory)); foreach (Model.Territories oTerritory in query) { oTerritory.TerritoryDescription = txtDescription.Text; oTerritory.RegionID = (int)cmbRegion.SelectedValue; } try { db.SubmitChanges(); MessageBox.Show("Successfully updated", "Update", MessageBoxButton.OK, MessageBoxImage.Information); this.Clean(); // Open the MenuTerritories MainWindow.StaticMainFrame.Content = new MenuTerritories(); } catch (Exception ex) { MessageBox.Show("error: " + ex.Message); } } }
private void btnDelete_Click(object sender, RoutedEventArgs e) { string idTerritory = (string)((Button)sender).CommandParameter; if (MessageBox.Show("Are you sure to delete?", "Confirm delete", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { using (var db = new Model.NorthwindDataContext()) { var query = db.Territories.Where(x => x.TerritoryID.Equals(idTerritory)); foreach (Model.Territories oTerritory in query) { oTerritory.habilitado = false; } try { db.SubmitChanges(); MessageBox.Show("Was successfully removed", "Delete", MessageBoxButton.OK, MessageBoxImage.Information); Refresh(); } catch (Exception ex) { MessageBox.Show("error: " + ex.Message); } } } }
void FillRegions() { using (var db = new Model.NorthwindDataContext()) { cmbRegion.ItemsSource = db.Region; cmbRegion.SelectedValuePath = "RegionID"; cmbRegion.DisplayMemberPath = "RegionDescription"; } }
void Refresh() { using (var db = new Model.NorthwindDataContext()) { dgvData.ItemsSource = (from t in db.Territories join r in db.Region on t.RegionID equals r.RegionID where t.habilitado.Equals(true) select new { t.TerritoryID, t.TerritoryDescription, Region = r.RegionDescription }).ToList(); } }
void AddTerritory() { using (var db = new Model.NorthwindDataContext()) { string idTerritory = txtTerritoryId.Text; if (IdExist(idTerritory)) { MessageBox.Show("TerritoryID already exists", "Exist", MessageBoxButton.OK, MessageBoxImage.Warning); txtTerritoryId.Focus(); return; } var oTerritory = new Model.Territories { TerritoryID = idTerritory, TerritoryDescription = txtDescription.Text, RegionID = (int)cmbRegion.SelectedValue }; db.Territories.InsertOnSubmit(oTerritory); try { db.SubmitChanges(); MessageBox.Show("Successfully registered", "Add", MessageBoxButton.OK, MessageBoxImage.Information); this.Clean(); // Open the MenuTerritories MainWindow.StaticMainFrame.Content = new MenuTerritories(); } catch (Exception ex) { MessageBox.Show("error: " + ex.Message); } } }