/// <summary> /// Gets a List of all Answers for a specific Question /// </summary> /// <returns>List<Questions> </returns> public static List<Answers> GetAllAnswerChoices(int questionID) { List<Answers> answerList = new List<Answers>(); string selectStatement = "Select * FROM answers WHERE questionID = @questionID"; try { using (SqlConnection connection = BrainFartConnection.GetConnection()) { connection.Open(); using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { selectCommand.Parameters.AddWithValue("@questionID", questionID); using (SqlDataReader reader = selectCommand.ExecuteReader()) { while (reader.Read()) { Answers answer = new Answers(); answer.AnswerID = (Int32)reader["answerID"]; answer.QuestionID = (Int32)reader["questionID"]; answer.AnswerDescrip = reader["answerDescrip"].ToString().Trim(); answer.Correct = (Int32)reader["correct"]; answerList.Add(answer); } } } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } return answerList; }
/// <summary> /// Insert a Answer row to the Answers table /// </summary> /// <param name="answer">The input Answer row object</param> /// <returns>The AnswerID for the row inserted</returns> /// public static int AddAnswer(Answers answer) { string insertStatement = "INSERT Answers " + "(AnswerDescrip, QuestionID, Correct) " + "VALUES (@AnswerDescrip, @QuestionID, @Correct)"; try { using (SqlConnection connection = BrainFartConnection.GetConnection()) { connection.Open(); using (SqlCommand insertCommand = new SqlCommand(insertStatement, connection)) { insertCommand.Parameters.AddWithValue("@AnswerDescrip", answer.AnswerDescrip); insertCommand.Parameters.AddWithValue("@QuestionID", answer.QuestionID); insertCommand.Parameters.AddWithValue("@Correct", answer.Correct); insertCommand.ExecuteNonQuery(); // Check for using statement use string selectStatement = "SELECT IDENT_CURRENT('Answers') FROM Answers"; // Check for backticks instead of single quotes using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { int answerID = Convert.ToInt32(selectCommand.ExecuteScalar()); return answerID; } } } } catch (SqlException se) { throw se; } catch (Exception ex) { throw ex; } }
private void btnSubmit_Click(object sender, EventArgs e) { try { if (Validator.IsPresent(txtQuestion) && Validator.IsPresent(txtAnswerA) && Validator.IsPresent(txtAnswerB) && Validator.IsPresent(txtAnswerC) && Validator.IsPresent(txtAnswerD) && Validator.IsPresent(cbCategory) && Validator.IsPresent(cbDifficulty) && (Validator.IsRadioButtonChecked(radioButton1, radioButton2, radioButton3, radioButton4))) { if (addQuestion) { question = new Questions(); answer1 = new Answers(); answer2 = new Answers(); answer3 = new Answers(); answer4 = new Answers(); this.putQuestionData(question); this.putAnswerData1(answer1); this.putAnswerData2(answer2); this.putAnswerData3(answer3); this.putAnswerData4(answer4); try { this.question.QuestionID = BrainFartController.AddQuestion(question); answer1.QuestionID = this.question.QuestionID; this.answer1.AnswerID = BrainFartController.AddAnswer(answer1); answer2.QuestionID = this.question.QuestionID; this.answer2.AnswerID = BrainFartController.AddAnswer(answer2); answer3.QuestionID = this.question.QuestionID; this.answer3.AnswerID = BrainFartController.AddAnswer(answer3); answer4.QuestionID = this.question.QuestionID; this.answer4.AnswerID = BrainFartController.AddAnswer(answer4); MessageBox.Show("Question submission successful"); this.BeginInvoke(new MethodInvoker(Close)); } catch (InvalidOperationException ioe) { throw ioe; } } else { newQuestion = new Questions(); this.putQuestionData(newQuestion); try { if (BrainFartController.UpdateQuestion(question, newQuestion)) { MessageBox.Show("Question Successfully updated to the system!", "BrainFart", MessageBoxButtons.OK, MessageBoxIcon.Information); updated = true; this.BeginInvoke(new MethodInvoker(Close)); } } catch (InvalidOperationException ioe) { throw ioe; } } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }
private void putAnswerData4(Answers answer4) { int correctAnswer; answer4.AnswerID = this.answer4.AnswerID; answer4.AnswerDescrip = txtAnswerD.Text; if (radioButton4.Checked) { correctAnswer = 1; } else { correctAnswer = 0; } answer4.Correct = correctAnswer; }
public static int AddAnswer(Answers answer) { return AnswersDAL.AddAnswer(answer); }
private void checkAnswer() { correctLabel.ForeColor = System.Drawing.Color.Green; if ((this.answerChoice1.Checked && answerList[0].Correct.Equals(1)) || (this.answerChoice2.Checked && answerList[1].Correct.Equals(1)) || (this.answerChoice3.Checked && answerList[2].Correct.Equals(1)) || (this.answerChoice4.Checked && answerList[3].Correct.Equals(1))) { correctLabel.Text = "Correct!"; this.correct = "Correct"; this.scoreLabel.Text = Convert.ToString(Int32.Parse(this.scoreLabel.Text) + Int32.Parse(this.pointValueLabel.Text)); } else { if (answerList[0].Correct.Equals(1)) { this.answer = answerList[0]; } else if (answerList[1].Correct.Equals(1)) { this.answer = answerList[1]; } else if (answerList[2].Correct.Equals(1)) { this.answer = answerList[2]; } else { this.answer = answerList[3]; } correctLabel.ForeColor = System.Drawing.Color.Red; correctLabel.Text = "Incorrect!"; this.correct = "Incorrect"; lblCorrectAnswer.Text = "The Correct Answer is: " + answer.AnswerDescrip; } }