private void btnOk_Click(object sender, RoutedEventArgs e) { int newId = sqlWorker.getNewId("Crime"); int oldId = 0; if (rbCrime.IsChecked == true && cbClause.SelectedItem != null && cbMilitaryUnit.SelectedItem != null && txStory.Text != "" && txDateRegistration.Text != "") { //заполнен минимум для преступления string accomplices = "", postAccomplice = ""; if (accompliceList != null) { for (int i = 0; i < accompliceList.values.Count; i++) { accomplices += (i == 0 ? "" : "\n") + accompliceList.values[i].Rank + " " + accompliceList.values[i].ShortName; //в идеале надо хранить в экземпляре Accomplice, но "и так сойдет" postAccomplice += (i == 0 ? "" : "\n") + sqlWorker.selectData("SELECT description FROM Post WHERE idPost = " + accompliceList.values[i].IdPost).Rows[0][0].ToString(); } } if (c != null) { oldId = c.Id; } c = new Crime( cbOrgan.SelectedItem == null?0:(cbOrgan.SelectedItem as KeyValue).Key, cbClause.SelectedItem == null?0:(cbClause.SelectedItem as Clause).Id, cbMilitaryUnit.SelectedItem == null ? 0 : (cbMilitaryUnit.SelectedItem as MilitaryUnit).Id, txDateRegistration.Text, txDateInstitution.Text, txDateCommit.Text, txStory.Text, txDamage.Text, txDateVerdict.Text, txVerdict.Text, txnumberCase.Text, postAccomplice, accomplices, cbClause.SelectedItem == null ? "" : (cbClause.SelectedItem as Clause).ToString(), cbClause.SelectedItem == null ? "" : (cbClause.SelectedItem as Clause).Number, chkIsRegistred.IsChecked == true ? 1:0, cbMilitaryUnit.SelectedItem == null ? "" : (cbMilitaryUnit.SelectedItem as MilitaryUnit).ShortName); } else if (rbCrime.IsChecked == false && cbMilitaryUnit.SelectedItem != null && txStory.Text != "" && txDateRegistration.Text != "") { //введен минимум для происшествия string accomplices = "", postAccomplice = ""; for (int i = 0; i < accompliceList.values.Count; i++) { accomplices += (i == 0 ? "" : "\n") + accompliceList.values[i].Rank + " " + accompliceList.values[i].ShortName; //в идеале надо хранить в экземпляре Accomplice, но "и так сойдет" postAccomplice += (i == 0 ? "" : "\n") + sqlWorker.selectData("SELECT description FROM Post WHERE idPost = " + accompliceList.values[i].IdPost).Rows[0][0].ToString(); } if (c != null) { oldId = c.Id; } c = new Crime( cbOrgan.SelectedItem == null ? 0 : (cbOrgan.SelectedItem as KeyValue).Key, cbClause.SelectedItem == null ? 0 : (cbClause.SelectedItem as Clause).Id, cbMilitaryUnit.SelectedItem == null ? 0 : (cbMilitaryUnit.SelectedItem as MilitaryUnit).Id, txDateRegistration.Text, txDateInstitution.Text, txDateCommit.Text, txStory.Text, txDamage.Text, txDateVerdict.Text, txVerdict.Text, txnumberCase.Text, postAccomplice, accomplices, cbClause.SelectedItem == null ? "" : (cbClause.SelectedItem as Clause).ToString(), cbClause.SelectedItem == null ? "" : (cbClause.SelectedItem as Clause).Number, chkIsRegistred.IsChecked == true ? 1 : 0, cbMilitaryUnit.SelectedItem == null ? "" : (cbMilitaryUnit.SelectedItem as MilitaryUnit).ShortName); } else { MessageBox.Show("Не все обязательные поля заполнены!"); return; } //похоже единственный случай когда изменения в бд нужно производить не из главного окна //т.к. нужно вносить изменения в 2 дополнительные таблицы - Portaking и InCategory c.Id = newId; if (!isEditing && sqlWorker.addCrime(c, accompliceList, categoryList)) { this.Close(); } else if (isEditing && sqlWorker.updateCrime(c, accompliceList, categoryList, oldId)) { this.Close(); } else { MessageBox.Show("Ошибка при добавлении преступления в базу данных"); } }