Exemple #1
0
 // Save changes back to the database and make them permanent
 private void saveChanges_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         schoolContext.SaveChanges();
         saveChanges.IsEnabled = false;
     }
     // Disse exceptions bliver først gennemgået senere, men det er langhåret. Det binder sig til
     // schoolContext, som er databasen.
     catch (OptimisticConcurrencyException ex)
     {
         // If the user has changed the same students earlier, then overwrite their changes with the new data
         this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext.Students);
         this.schoolContext.SaveChanges();
     }
     catch (UpdateException uEx)
     {
         MessageBox.Show("Error occured when saving changes:\n" + uEx.InnerException.Message
                         , "Error saving changes");
         this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext.Students);
     }
     catch (Exception ex)
     {
         MessageBox.Show("An error occured:\n" + ex.InnerException.Message
                         , "Error saving changes");
         this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext.Students);
     }
 }
Exemple #2
0
        // Save changes back to the database and make them permanent
        private void saveChanges_Click(object sender, RoutedEventArgs e)
        {
            // TODO: Exercise 3: Task 2b: Save the changes by calling the SaveChanges method of the schoolContext object
            // TODO: Exercise 3: Task 3a: If an OptimisticConcurrencyException occurs then another user has changed the same students earlier, then overwrite their changes with the new data (see the lab instructions for details)
            // TODO: Exercise 3: Task 3b: If an UpdateException occurs then report the error to the user and rollback (see the lab instructions for details)
            // TODO: Exercise 3: Task 3c: If some other sort of error has occurs, report the error to the user and retain the data so the user can try again - the error may be transitory (see the lab instructions for details)
            StringBuilder messages = new StringBuilder();

            try
            {
                schoolContext.SaveChanges();
            }
            catch (OptimisticConcurrencyException ocex)
            {
                messages.AppendLine("Optimistic concurrency exception");
                this.schoolContext.Refresh(RefreshMode.ClientWins, schoolContext);
            }
            catch (UpdateException updex)
            {
                messages.AppendLine("Update exception");
                this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext);
            }
            catch (Exception ex)
            {
                messages.AppendLine("È successo un Errore");
            }

            if (messages.Length > 0)
            {
                MessageBox.Show(messages.ToString());
            }

            saveChanges.IsEnabled = false;
        }
        // Save changes back to the database and make them permanent
        private void saveChanges_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                schoolContext.SaveChanges();
                saveChanges.IsEnabled = false;
            }
            catch (OptimisticConcurrencyException)
            {
                this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext.Students);
                this.schoolContext.SaveChanges();
            }
            catch (UpdateException uEx)
            {
                MessageBox.Show(uEx.InnerException.Message, "Error saving changes");
                this.schoolContext.Refresh(RefreshMode.StoreWins, schoolContext.Students);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error saving changes");
                this.schoolContext.Refresh(RefreshMode.ClientWins, schoolContext.Students);
            }
            saveChanges.IsEnabled = false;

            // TODO: Exercise 3: Task 2b: Save the changes by calling the SaveChanges method of the schoolContext object
            // TODO: Exercise 3: Task 3a: If an OptimisticConcurrencyException occurs then another user has changed the same students earlier, then overwrite their changes with the new data (see the lab instructions for details)
            // TODO: Exercise 3: Task 3b: If an UpdateException occurs then report the error to the user and rollback (see the lab instructions for details)
            // TODO: Exercise 3: Task 3c: If some other sort of error has occurs, report the error to the user and retain the data so the user can try again - the error may be transitory (see the lab instructions for details)
        }
 // Save changes back to the database and make them permanent
 private void saveChanges_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         schoolContext.SaveChanges();
         this.saveChanges.IsEnabled = false;
     }
     catch (OptimisticConcurrencyException exc)
     {
         this.schoolContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, schoolContext.Students);
         this.schoolContext.SaveChanges();
     }
     catch (UpdateException exc)
     {
         MessageBox.Show(exc.InnerException.Message, "Error saving changes");
         this.schoolContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, schoolContext.Students);
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.Message, "Error saving changes");
         this.schoolContext.Refresh(System.Data.Objects.RefreshMode.ClientWins, schoolContext.Students);
     }
 }
Exemple #5
0
 private void saveChanges_Click(object sender, RoutedEventArgs e)
 {
     schoolContext.SaveChanges();
     saveChanges.IsEnabled = false;
 }