/// <summary> /// Gets a List of all questions /// </summary> /// <returns>List<Questions> </returns> public static List<Questions> GetAllQuestions() { List<Questions> questionList = new List<Questions>(); string selectStatement = "Select * FROM questions"; try { using (SqlConnection connection = BrainFartConnection.GetConnection()) { connection.Open(); using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { using (SqlDataReader reader = selectCommand.ExecuteReader()) { while (reader.Read()) { Questions question = new Questions(); question.QuestionID = (Int32)reader["questionID"]; question.QuestionDescrip = reader["questionDescrip"].ToString().Trim(); question.CategoryID = (Int32)reader["categoryID"]; question.DifficultyID = (Int32)reader["difficultyID"]; questionList.Add(question); } } } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } return questionList; }
/// <summary> /// Insert a Question row to the Questions table /// </summary> /// <param name="question">The input Question row object</param> /// <returns>The QuestionID for the row inserted</returns> /// public static int AddQuestion(Questions question) { string insertStatement = "INSERT Questions " + "(QuestionDescrip, CategoryID, DifficultyID) " + "VALUES (@QuestionDescrip, @CategoryID, @DifficultyID)"; try { using (SqlConnection connection = BrainFartConnection.GetConnection()) { connection.Open(); using (SqlCommand insertCommand = new SqlCommand(insertStatement, connection)) { insertCommand.Parameters.AddWithValue("@QuestionDescrip", question.QuestionDescrip); insertCommand.Parameters.AddWithValue("@CategoryID", question.CategoryID); insertCommand.Parameters.AddWithValue("@DifficultyID", question.DifficultyID); insertCommand.ExecuteNonQuery(); // Check for using statement use string selectStatement = "SELECT IDENT_CURRENT('Questions') FROM Questions"; // Check for backticks instead of single quotes using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { int questionID = Convert.ToInt32(selectCommand.ExecuteScalar()); return questionID; } } } } 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()); } }
/// <summary> /// Insert question information /// </summary> /// <param name="question"></param> private void putQuestionData(Questions question) { question.QuestionID = this.question.QuestionID; question.QuestionDescrip = txtQuestion.Text; question.CategoryID = (int)cbCategory.SelectedValue; question.DifficultyID = (int)cbDifficulty.SelectedValue; }
public static bool UpdateQuestion(Questions question, Questions newQuestion) { throw new NotImplementedException(); }
public static int AddQuestion(Questions question) { return QuestionsDAL.AddQuestion(question); }
/// <summary> /// Gets a List of all questions in a particular category /// </summary> /// <returns>List<Questions> </returns> public static List<Questions> GetQuestionsFromCategoryWithDifficulty(int categoryID, int difficultyID) { List<Questions> questionList = new List<Questions>(); string selectStatement = "Select * FROM questions WHERE categoryID = @categoryID AND difficultyID = @difficultyID"; try { using (SqlConnection connection = BrainFartConnection.GetConnection()) { connection.Open(); using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { selectCommand.Parameters.AddWithValue("categoryID", categoryID); selectCommand.Parameters.AddWithValue("difficultyID", difficultyID); using (SqlDataReader reader = selectCommand.ExecuteReader()) { while (reader.Read()) { Questions question = new Questions(); question.QuestionID = (Int32)reader["questionID"]; question.QuestionDescrip = reader["questionDescrip"].ToString().Trim(); question.CategoryID = (Int32)reader["categoryID"]; question.DifficultyID = (Int32)reader["difficultyID"]; questionList.Add(question); } } } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } return questionList; }
private void getQuestion() { //Get a random question from the list of questions Random rnd = new Random(); int r = rnd.Next(this.questionList.Count); this.question = questionList[r]; }