void Initialize() { //CLEAN CODE WRAPPER - BETTER using (var db = new newrabbitdatabaseEntities()) { rabbits = db.rabbittables.ToList(); //foreach (var item in rabbits) //{ // ListViewRabbits.Items.Add($"ID: {item.RabbitTableID} Name: {item.RabbitName} DOB: {item.RabbitDOB} Age: {item.RabbitAge} Type: {item.RabbitType} Is Active: {item.RabbitIsActive}"); //} } // db not valid here TextBoxRabbitID.IsReadOnly = true; TextBoxRabbitName.IsReadOnly = true; TextBoxRabbitAge.IsReadOnly = true; ButtonEdit.IsEnabled = false; }
private void ListViewRabbits_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { if (Rabbit != null) { using (var db = new newrabbitdatabaseEntities()) { var rabbitToDelete = db.rabbittables.Find(Rabbit.RabbitTableID); var result = MessageBox.Show($"Delete {Rabbit.RabbitName}? Are you sure?"); if (result == MessageBoxResult.OK) { db.rabbittables.Remove(rabbitToDelete); db.SaveChanges(); ListViewRabbits.ItemsSource = null; rabbits = db.rabbittables.ToList(); ListViewRabbits.ItemsSource = rabbits; } } } }
private void ButtonAddRabbits_Click(object sender, RoutedEventArgs e) { if (ButtonAddRabbits.Content.ToString() == "Add Rabbit") { TextBoxRabbitName.IsReadOnly = false; TextBoxRabbitAge.IsReadOnly = false; TextBoxRabbitID.Text = ""; TextBoxRabbitName.Text = ""; TextBoxRabbitAge.Text = ""; ButtonAddRabbits.Content = "Save"; } else { if (TextBoxRabbitName.Text.Length > 0) { Int32.TryParse(TextBoxRabbitAge.Text, out int rabbitAge); var rabbitToAdd = new rabbittable() { RabbitName = TextBoxRabbitName.Text, RabbitAge = rabbitAge, }; using (var db = new newrabbitdatabaseEntities()) { db.rabbittables.Add(rabbitToAdd); db.SaveChanges(); ListViewRabbits.ItemsSource = null; rabbits = db.rabbittables.ToList(); ListViewRabbits.ItemsSource = rabbits; } } TextBoxRabbitName.IsReadOnly = true; TextBoxRabbitAge.IsReadOnly = true; ButtonAddRabbits.Content = "Add Rabbit"; } }