private void button_Click(object sender, RoutedEventArgs e) { if (MessageBox.Show(String.Format("Питання буде {0} у базі. Ви впевнені?", ID == 0 ? "збережено" : "змінено"), "Увага", MessageBoxButton.OKCancel) != MessageBoxResult.OK) { return; } if (String.IsNullOrWhiteSpace(textBoxLevel.Text)) { MessageBox.Show("Перевірте поле Рівень"); return; } if (String.IsNullOrWhiteSpace(textBoxTopic.Text)) { MessageBox.Show("Перевірте поле Тема "); return; } if (String.IsNullOrWhiteSpace(textBoxQuestion.Text)) { MessageBox.Show("Перевірте поле питання "); return; } int idTick = 0; using (DBWokrSql db = new DBWokrSql(Connect)) { if (ID == 0) { //create question idTick = db.AddNewQuestion( new QuestionInfo() { ID = 0, Topic = textBoxTopic.Text, Info = textBoxInfo.Text, Level = textBoxLevel.Text, Question = textBoxQuestion.Text }, ans ); } else { //update question var questionCurrent = db.GetQuestion(ID); if ( !(questionCurrent.Info.Equals(textBoxInfo.Text.Replace('\'', ' ')) && questionCurrent.Level.Equals(textBoxLevel.Text) && questionCurrent.Question.Equals(textBoxQuestion.Text.Replace('\'', ' ')) && questionCurrent.Topic.Equals(textBoxTopic.Text)) ) { idTick = db.UpdateQuestion(questionCurrent); } if (idTick == 0) //question didn`t change { //update answers var answersCurrent = db.GetAnswers(ID); //get all new answers and check them by similar in database, if they were not found -- add them foreach (var item in ans) { if (answersCurrent.IsGetValue(item)) { var ss = answersCurrent.Where(s => s.ID == item.ID).First(); if (ss.IsTrue == item.IsTrue && ss.Text.Equals(item.Text.Replace('\'', ' '))) { continue; } else { db.UpdateAnswer(ID, item); } } else { int idAnswer = db.AddAnswer(item.Text); db.AddQuestionAnswer(ID, idAnswer, item.IsTrue); } } //marked all answers that not use foreach (var item in answersCurrent) { if (!ans.IsGetValue(item)) { db.DisableAnswer(item.ID); } } } else {// add answers to new question foreach (var item in ans) { int idAnswer = db.AddAnswer(item.Text); db.AddQuestionAnswer(idTick, idAnswer, item.IsTrue); } } } } MessageBox.Show("Питання було успішно збережено під номером " + (idTick == 0 ? ID : idTick)); }