// Сохранение информации о жителе private void saveButton_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Вы уверены, что хотите сохранить изменения?\n", "Предупреждение", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { if (isAllFieldsValid() == false) { return; } try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; db = new DataContext(sqlConnectionString.ConnectionString); Resident resident = db.GetTable <Resident>().SingleOrDefault(r => r.ResidentId == residentId); Resident oldResident = resident.ShallowCopy(); int organizationId = Int32.Parse(organizationIdLabel.Text); resident.Surname = surnameTextBox.Text; resident.Name = nameTextBox.Text; resident.Patronymic = patronymicTextBox.Text; resident.PhoneNumber = phoneNumberTextBox.Text; resident.Birthday = birthdayDateTimePicker.NullableValue(); resident.Note = noteTextBox.Text; resident.Organization = db.GetTable <Organization>().SingleOrDefault(r => r.OrganizationId == organizationId); Passport passport = db.GetTable <Passport>().SingleOrDefault(p => p.PassportId == resident.PassportId); oldResident.Passport = passport.ShallowCopy(); passport.Number = passportNumberTextBox.Text; passport.Series = passportSeriesTextBox.Text; passport.Registration = passportRegistrationTextBox.Text; passport.DateOfIssue = passportDateOfIssueDateTimePicker.Value.Date; passport.Authority = passportAuthorityTextBox.Text; SqlConnectionStringBuilder sConnBForOtherServer = new SqlConnectionStringBuilder() { DataSource = Properties.Settings.Default.userServer2Name, InitialCatalog = Properties.Settings.Default.userServer2Database, UserID = sqlConnectionString.UserID, Password = sqlConnectionString.Password, }; DataContext dbFromOtherServer = new DataContext(sConnBForOtherServer.ConnectionString); Resident resident2 = dbFromOtherServer.GetTable <Resident>().SingleOrDefault(r => r.ResidentId == residentId); resident2.Surname = surnameTextBox.Text; resident2.Name = nameTextBox.Text; resident2.Patronymic = patronymicTextBox.Text; resident2.PhoneNumber = phoneNumberTextBox.Text; resident2.Birthday = birthdayDateTimePicker.NullableValue(); resident2.Note = noteTextBox.Text; resident2.Organization = dbFromOtherServer.GetTable <Organization>().SingleOrDefault(r => r.OrganizationId == organizationId); Passport passport2 = dbFromOtherServer.GetTable <Passport>().SingleOrDefault(p => p.PassportId == resident2.PassportId); passport2.Number = passportNumberTextBox.Text; passport2.Series = passportSeriesTextBox.Text; passport2.Registration = passportRegistrationTextBox.Text; passport2.DateOfIssue = passportDateOfIssueDateTimePicker.Value.Date; passport2.Authority = passportAuthorityTextBox.Text; dbFromOtherServer.SubmitChanges(); db.SubmitChanges(); SystemSounds.Beep.Play(); MessageBox.Show("Успешно сохранено!"); HistoryRecordsController.WriteAboutEditResident(oldResident, resident); } catch (Exception ex) { SystemSounds.Exclamation.Play(); HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при сохранении изменений жителя."); MessageBox.Show("Ошибка при сохранении изменений. \nВызвано исключение: " + ex.Message); } finally { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } } }
// Удаляет выбранного жителя private void deleteButton_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Вы уверены, что хотите удалить выбранного жителя?\n", "Предупреждение", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; Int32.TryParse(residentIdLabel.Text, out int residentId); if (residentId == 0) { MessageBox.Show("Выберите жителя"); } else { SqlConnectionStringBuilder sConnBForOtherServer = new SqlConnectionStringBuilder() { DataSource = Properties.Settings.Default.userServer2Name, InitialCatalog = Properties.Settings.Default.userServer2Database, UserID = sqlConnectionString.UserID, Password = sqlConnectionString.Password, }; DataContext dbFromOtherServer = new DataContext(sConnBForOtherServer.ConnectionString); Resident resident2 = dbFromOtherServer.GetTable <Resident>().FirstOrDefault(r => r.ResidentId == residentId); if (resident2 != null) { dbFromOtherServer.GetTable <Resident>().DeleteOnSubmit(resident2); } Passport passport2 = dbFromOtherServer.GetTable <Passport>().FirstOrDefault(r => (r.PassportId == resident2.PassportId)); if (passport2 != null) { resident2.Passport = passport2; dbFromOtherServer.GetTable <Passport>().DeleteOnSubmit(passport2); } RoomResidents roomResidents2 = dbFromOtherServer.GetTable <RoomResidents>().FirstOrDefault(r => (r.ResidentId == residentId)); if (roomResidents2 != null) { dbFromOtherServer.GetTable <RoomResidents>().DeleteOnSubmit(roomResidents2); } ResidentRooms residentRooms2 = dbFromOtherServer.GetTable <ResidentRooms>() .FirstOrDefault(r => (r.ResidentId == residentId && r.DateOfEviction == null)); if (residentRooms2 != null) { residentRooms2.DateOfEviction = DateTime.Now; } db = new DataContext(sqlConnectionString.ConnectionString); Resident resident = db.GetTable <Resident>().FirstOrDefault(r => r.ResidentId == residentId); if (resident != null) { db.GetTable <Resident>().DeleteOnSubmit(resident); } Passport passport = db.GetTable <Passport>().FirstOrDefault(r => (r.PassportId == resident.PassportId)); if (passport != null) { resident.Passport = passport; db.GetTable <Passport>().DeleteOnSubmit(passport); } RoomResidents roomResidents = db.GetTable <RoomResidents>().FirstOrDefault(r => (r.ResidentId == residentId)); if (roomResidents != null) { db.GetTable <RoomResidents>().DeleteOnSubmit(roomResidents); } ResidentRooms residentRooms = db.GetTable <ResidentRooms>() .FirstOrDefault(r => (r.ResidentId == residentId && r.DateOfEviction == null)); if (residentRooms != null) { residentRooms.DateOfEviction = DateTime.Now; } dbFromOtherServer.SubmitChanges(); db.SubmitChanges(); LoadDataGrid(); HistoryRecordsController.WriteAboutAddDeleteResident(resident, false); } } catch (Exception ex) { HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при удалении жителя в deleteButton_Click."); MessageBox.Show("Ошибка при удалении жителя.\nВызвано исключение: " + ex.Message); } finally { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } } }
// Добавление нового жителя private void addButton_Click(object sender, EventArgs e) { DialogResult dialogResult = MessageBox.Show("Вы уверены, что хотите добавить данного жителя?\n", "Предупреждение", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { if (isAllFieldsValid() == false) { return; } try { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor; // Для сброса контекста, если была попытка ввода нового значения db = new DataContext(sqlConnectionString.ConnectionString); string surname = surnameTextBox.Text; string name = nameTextBox.Text; string patronymic = patronymicTextBox.Text; string phoneNumber = phoneNumberTextBox.Text; DateTime?birthday = birthdayDateTimePicker.NullableValue(); string passportSeries = passportSeriesTextBox.Text; string passportNumber = passportNumberTextBox.Text; string passportRegistration = passportRegistrationTextBox.Text; string note = noteTextBox.Text; DateTime passportDateOfIssue = passportDateOfIssueDateTimePicker.Value.Date; string passportAuthority = passportAuthorityTextBox.Text; int organizationId = Int32.Parse(organizationIdLabel.Text); Organization organization = db.GetTable <Organization>().SingleOrDefault(r => r.OrganizationId == organizationId); Passport passport = new Passport { Series = passportSeries, Number = passportNumber, Registration = passportRegistration, DateOfIssue = passportDateOfIssue, Authority = passportAuthority }; db.GetTable <Passport>().InsertOnSubmit(passport); Resident resident = new Resident { Surname = surname, Name = name, Patronymic = patronymic, PhoneNumber = phoneNumber, Birthday = birthday, Note = note, Passport = passport, Organization = organization, }; db.GetTable <Resident>().InsertOnSubmit(resident); SqlConnectionStringBuilder sConnBForOtherServer = new SqlConnectionStringBuilder() { DataSource = Properties.Settings.Default.userServer2Name, InitialCatalog = Properties.Settings.Default.userServer2Database, UserID = sqlConnectionString.UserID, Password = sqlConnectionString.Password, }; DataContext dbFromOtherServer = new DataContext(sConnBForOtherServer.ConnectionString); Organization organization2 = dbFromOtherServer.GetTable <Organization>().SingleOrDefault(r => r.OrganizationId == organizationId); Passport passport2 = new Passport { Series = passportSeries, Number = passportNumber, Registration = passportRegistration, DateOfIssue = passportDateOfIssue, Authority = passportAuthority }; dbFromOtherServer.GetTable <Passport>().InsertOnSubmit(passport2); Resident resident2 = new Resident { Surname = surname, Name = name, Patronymic = patronymic, PhoneNumber = phoneNumber, Birthday = birthday, Note = note, Passport = passport2, Organization = organization2, }; dbFromOtherServer.GetTable <Resident>().InsertOnSubmit(resident2); dbFromOtherServer.SubmitChanges(); db.SubmitChanges(); SystemSounds.Beep.Play(); MessageBox.Show("Успешно добавлен!"); HistoryRecordsController.WriteAboutAddDeleteResident(resident, true); Close(); } catch (Exception ex) { SystemSounds.Exclamation.Play(); HistoryRecordsController.WriteExceptionToLogFile(ex, "Ошибка при добавлении жителя."); MessageBox.Show("Ошибка при добавлении жителя.\nВызвано исключение: " + ex.Message); } finally { System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default; } } }