private void button__Add_Click(object sender, EventArgs e) { if (!this.saveAnswers(false)) { return; } using (Tests_DBContainer tests = new Tests_DBContainer()) { var answer = new TestQuestionAnswer() { Answer = "", TestQuestionId = questionId, IsAnswer = 0 }; tests.TestQuestionAnswer.Add(answer); tests.SaveChanges(); renderAnswerList(); } }
private void button_AddNewQuestion_Click(object sender, EventArgs e) { bool error = false; using (Tests_DBContainer tests = new Tests_DBContainer()) { if (textBox_AddQuestion.Text == "") { MessageBox.Show("Введите вопрос"); error = true; } if (checkedListBox_QuestionVariants.Items.Count == 0) { MessageBox.Show("Добавьте ответы"); error = true; } if (checkedListBox_QuestionVariants.SelectedItem == null || checkedListBox_QuestionVariants.CheckedItems.Count == 0) { MessageBox.Show("Выберите верный ответ"); error = true; } if (tests.TestQuestion.Where(t => t.TestId == this.testId && t.Question == textBox_AddQuestion.Text).ToList().Count > 0) { MessageBox.Show("Есть такой вопрос"); error = true; } if (!error) { TestQuestion testquestion = new TestQuestion(); testquestion.Question = textBox_AddQuestion.Text; testquestion.IsActual = 0; if (checkBox_QuestionTrue.Checked) { testquestion.IsActual = 1; } testquestion.TestId = this.testId; tests.TestQuestion.Add(testquestion); int i; for (i = 0; i <= (checkedListBox_QuestionVariants.Items.Count - 1); i++) { TestQuestionAnswer answer = new TestQuestionAnswer(); answer.Answer = checkedListBox_QuestionVariants.Items[i].ToString(); if (checkedListBox_QuestionVariants.GetItemChecked(i)) { answer.IsAnswer = Convert.ToByte(true); } answer.TestQuestion = testquestion; tests.TestQuestionAnswer.Add(answer); } tests.SaveChanges(); checkedListBox_QuestionVariants.Items.Clear(); textBox_AddQuestion.Text = ""; checkBox_QuestionTrue.Checked = true; renderListQuestions((comboBox_SelectQuestion.Items.Count)); } } }