/// <summary> /// Deletes the selected Account from Accounts Database and update the data in ListView /// </summary> /// <param name="sender">Button Control</param> /// <param name="e">Evenet handler delegate</param> private void DeleteButton_Click(object sender, RoutedEventArgs e) { // Create an Account object from the selected item of the ListView Account selectedAccount = AccountList.SelectedItem as Account; // Get the Account Id property int accountID = selectedAccount.Id; // Create new DataContext object using (AccountsDataContext accountsDataBase = new AccountsDataContext()) { // Get from the Database the Account which had the same Id as the Account selected in ListView Account accountToDelete = accountsDataBase.Accounts.SingleOrDefault(item => item.Id == accountID); // Delete the Account from Accounts Databse accountsDataBase.Accounts.DeleteOnSubmit(accountToDelete); // Submit changes into Accounts Database accountsDataBase.SubmitChanges(); } // Reset window to default state ResetWindow(); // Display message for user about the succefull completion of transacton MessageBox.Show("Account is successfully removed from the list.", "Success", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// Updates the selected Account from ListView in Database with new data in respective Textboxes. /// </summary> /// <param name="sender">Button Control</param> /// <param name="e">Evenet handler delegate</param> private void UpdateButton_Click(object sender, RoutedEventArgs e) { // Create an Account object from the selected item of the ListView Account selectedAccount = AccountList.SelectedItem as Account; // Get the Account Id property int accountID = selectedAccount.Id; // Get the updated data from respective TextBoxes string firstName = FirstNameTextBox.Text; string lastName = LastNameTextBox.Text; string passportID = PassportTextBox.Text; // Create new DataContext object using (AccountsDataContext accountsDataBase = new AccountsDataContext()) { // Get from the Database the Account which had the same Id as the Account selected in ListView Account newAccountInstance = accountsDataBase.Accounts.SingleOrDefault(item => item.Id == accountID); // Updated the Account in Databse newAccountInstance.First_Name = firstName; newAccountInstance.Last_Name = lastName; newAccountInstance.Passport = passportID; // Submit changes into Accounts Database accountsDataBase.SubmitChanges(); } // Reset window to default state ResetWindow(); // Display message for user about the succefull completion of transacton MessageBox.Show("Account data is successfully updated .", "Success", MessageBoxButton.OK, MessageBoxImage.Information); }
/// <summary> /// Gets the text input from TextBoxes, creates an Account Entity and inserts it into Accounts Database /// </summary> /// <param name="sender">Button Control</param> /// <param name="e">Evenet handler delegate</param> private void CreateButton_Click(object sender, RoutedEventArgs e) { // Show warning message in case not all the required textboxes contain data if (String.IsNullOrEmpty(FirstNameTextBox.Text) || String.IsNullOrEmpty(LastNameTextBox.Text) || String.IsNullOrEmpty(PassportTextBox.Text)) { MessageBox.Show("Please, complete the form.", "Incomplete data", MessageBoxButton.OK, MessageBoxImage.Warning); } // If all textboces contain data, create a new Account object with the data from textboxes else { // Create variables with the filled-in text from respective TextBoxes string firstName = FirstNameTextBox.Text; string lastName = LastNameTextBox.Text; string passportID = PassportTextBox.Text; // Create AccountsDataContext class instance using (AccountsDataContext accountsDataBase = new AccountsDataContext()) { // Create a new Account instance that should be inserted into Accounts Database Account accountToInsert = new Account { First_Name = firstName, Last_Name = lastName, Passport = passportID }; // Insert the created Account instance into Acocunts Table accountsDataBase.Accounts.InsertOnSubmit(accountToInsert); // Submit changes into Accounts Database accountsDataBase.SubmitChanges(); } // Reset window to default state ResetWindow(); } }