コード例 #1
0
 private void btUpdate_Click(object sender, RoutedEventArgs e)
 {
     if (lstViewOwner.SelectedItems.Count != 1)
     {
         MessageBox.Show("Please choose just one item to update", "Information");
         return;
     }
     try
     {
         Owner curentOwner = (Owner)lstViewOwner.SelectedItem;
         using (var ctx = new CarsOwnerDbContext())
         {
             Owner toUpdate = (from o in ctx.Owners where o.OwnerId == curentOwner.OwnerId select o).FirstOrDefault <Owner>();
             if (toUpdate != null)
             {
                 toUpdate.Name = tbName.Text;
                 if (!string.IsNullOrEmpty(imageLocation))
                 {
                     toUpdate.Photo = File.ReadAllBytes(imageLocation);
                 }
                 ctx.SaveChanges();
                 MessageBox.Show("Owner Updated");
                 resetAndRefresh();
             }
             else
             {
                 MessageBox.Show("Record to update not found");
             }
         }
     }
     catch (Exception ex) when(ex is IOException || ex is FileNotFoundException || ex is SqlException)
     {
         MessageBox.Show(" ERROR Update Owner: " + ex.Message, "Error Information");
     }
 }
コード例 #2
0
 private void btAdd_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (string.IsNullOrEmpty(tbName.Text) || string.IsNullOrEmpty(imageLocation))
         {
             MessageBox.Show("Please input Name and choose image");
             return;
         }
         byte[] image    = File.ReadAllBytes(imageLocation);
         Owner  newOwner = new Owner {
             Name = tbName.Text, Photo = image
         };
         using (var ctx = new CarsOwnerDbContext())
         {
             ctx.Owners.Add(newOwner);
             ctx.SaveChanges();
             MessageBox.Show("Owner Added");
             resetAndRefresh();
         }
     }
     catch (Exception ex) when(ex is IOException || ex is FileNotFoundException || ex is SqlException)
     {
         MessageBox.Show("Error adding Owner to database:\n" + ex.Message, "Error Information");
     }
 }
コード例 #3
0
 private void btAdd_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (string.IsNullOrEmpty(tbModel.Text))
         {
             MessageBox.Show("Please input Model");
             return;
         }
         Car newCar = new Car {
             MakeModel = tbModel.Text, OwnerId = currentOwner.OwnerId
         };
         using (var ctx = new CarsOwnerDbContext())
         {
             ctx.Cars.Add(newCar);
             currentOwner.CarsInGarage.Add(newCar);
             ctx.SaveChanges();
             MessageBox.Show("Car Added");
             resetAndRefresh(currentOwner);
         }
     }
     catch (SqlException ex)
     {
         MessageBox.Show(" ERROR Select Car: " + ex.Message, "Error Information");
     }
 }
コード例 #4
0
 private void btUpdate_Click(object sender, RoutedEventArgs e)
 {
     if (lstViewCar.SelectedItems.Count != 1)
     {
         MessageBox.Show("Please choose just one item to update", "Information");
         return;
     }
     try
     {
         Car currentCar = (Car)lstViewCar.SelectedItem;
         using (var ctx = new CarsOwnerDbContext())
         {
             Car toUpdate = ctx.Cars.Include("Owner").Where(c => c.CarId == currentCar.CarId).FirstOrDefault <Car>();
             if (toUpdate != null)
             {
                 toUpdate.MakeModel = tbModel.Text;
                 ctx.SaveChanges();
                 MessageBox.Show("Car Updated");
                 resetAndRefresh(currentOwner);
             }
             else
             {
                 MessageBox.Show("Record to update not found");
             }
         }
     }
     catch (SqlException ex)
     {
         MessageBox.Show(" ERROR Update Owner: " + ex.Message, "Error Information");
     }
 }
コード例 #5
0
        private void btDelete_Click(object sender, RoutedEventArgs e)
        {
            if (lstViewCar.SelectedItems.Count != 1)
            {
                MessageBox.Show("Please choose just one item to delete", "Information");
                return;
            }
            MessageBoxResult messageBoxResult = MessageBox.Show("Are you sure to delete?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);

            try
            {
                if (messageBoxResult == MessageBoxResult.Yes)
                {
                    Car currentCar = (Car)lstViewCar.SelectedItem;

                    using (var ctx = new CarsOwnerDbContext())
                    {
                        Car toDelete = ctx.Cars.Include("Owner").Where(c => c.CarId == currentCar.CarId).FirstOrDefault <Car>();
                        if (toDelete != null)
                        {
                            ctx.Cars.Remove(toDelete);
                            ctx.SaveChanges();
                            MessageBox.Show("Car Delete");
                            resetAndRefresh(currentOwner);
                        }
                        else
                        {
                            MessageBox.Show("Record to update not found");
                        }
                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(" ERROR Update Owner: " + ex.Message, "Error Information");
            }
        }