public string insertQuizAnswer(QuizAnswer _newQuizAnswer) { DataTable _dataBaseResults = new DataTable(); SqlConnectionStringBuilder csBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["FarmWorkerAppDB"].ConnectionString); string _dataSource = csBuilder.ToString(); SqlConnection myConnection = new SqlConnection(_dataSource); String query = "InsertQuizAnswer"; SqlCommand command = new SqlCommand(query, myConnection); command.CommandType = CommandType.StoredProcedure; if (readLatestQuizID() == "0") { return("An error Ocurred in the Data Base"); } if (_newQuizAnswer.AnswerID <= 0) { return("Question ID can not be negative or Zero"); } //--Data of QuizQuestion Table-- command.Parameters.AddWithValue("@QuizID", _newQuizAnswer.QuizID == 0 ? readLatestQuizID() : _newQuizAnswer.QuizID.ToString()); command.Parameters.AddWithValue("@QuizVersion", _newQuizAnswer.QuizVersion == 0 ? readLatestQuizVersion(_newQuizAnswer.QuizID.ToString()) == "0" ? "1" : readLatestQuizVersion(_newQuizAnswer.QuizID.ToString()) : _newQuizAnswer.QuizVersion.ToString()); command.Parameters.AddWithValue("@QuestionID", _newQuizAnswer.QuestionID); command.Parameters.AddWithValue("@AnswerID", _newQuizAnswer.AnswerID); command.Parameters.AddWithValue("@Answer", _newQuizAnswer.Answer == null || _newQuizAnswer.Answer == "" ? "Answer Undefined" : _newQuizAnswer.Answer); command.Parameters.AddWithValue("@IsCorrect", _newQuizAnswer.IsCorrect); command.Parameters.AddWithValue("@IsActive", _newQuizAnswer.IsActive); command.Parameters.AddWithValue("@DateModified", DateTime.Now.ToShortDateString()); List <QuizQuestion> _listOfQuizQuestions = new List <QuizQuestion>(); try { myConnection.Open(); command.ExecuteNonQuery(); myConnection.Close(); } catch (Exception _exceptionData) { return("An error Ocurred in the Data Base"); } //Insert Answers return("Quiz Answer Inserted Successfully"); }
public List <QuizAnswer> readAllQuizQuestionAnswers(Int64 _quizID, Int64 _quizVersion, Int64 _questionID) { DataTable _dataBaseResults = new DataTable(); SqlConnectionStringBuilder csBuilder = new SqlConnectionStringBuilder(ConfigurationManager.ConnectionStrings["FarmWorkerAppDB"].ConnectionString); string _dataSource = csBuilder.ToString(); SqlConnection myConnection = new SqlConnection(_dataSource); String query = "ReadQuizAnswersByID&Version"; SqlCommand command = new SqlCommand(query, myConnection); command.CommandType = CommandType.StoredProcedure; //--Data of User Table-- command.Parameters.AddWithValue("@QuizID", _quizID); command.Parameters.AddWithValue("@QuizVersion", _quizVersion); command.Parameters.AddWithValue("@QuizQuestionID", _questionID); List <QuizAnswer> _listOfQuizQuestionAnswers = new List <QuizAnswer>(); try { myConnection.Open(); _dataBaseResults.Load(command.ExecuteReader()); myConnection.Close(); } catch (Exception _exceptionData) { return(_listOfQuizQuestionAnswers); } foreach (DataRow _row in _dataBaseResults.Rows) { QuizAnswer _newQuizQuestionAnswer = new QuizAnswer(); _newQuizQuestionAnswer.QuestionID = _row["QuestionID"].ToString() != "" ? Int64.Parse(_row["QuestionID"].ToString()) : 0; _newQuizQuestionAnswer.QuizVersion = _row["QuizVersion"].ToString() != "" ? Int64.Parse(_row["QuizVersion"].ToString()) : 0; _newQuizQuestionAnswer.QuizID = _row["QuizID"].ToString() != "" ? Int64.Parse(_row["QuizID"].ToString()) : 0; _newQuizQuestionAnswer.AnswerID = _row["AnswerID"].ToString() != "" ? Int64.Parse(_row["AnswerID"].ToString()) : 0; _newQuizQuestionAnswer.Answer = _row["Answer"].ToString(); _newQuizQuestionAnswer.IsActive = _row["IsActive"].ToString(); _newQuizQuestionAnswer.IsCorrect = _row["IsCorrect"].ToString(); _newQuizQuestionAnswer.DateModified = _row["DateModified"].ToString(); if (_newQuizQuestionAnswer.QuestionID != 0 && _newQuizQuestionAnswer.QuizVersion != 0 && _newQuizQuestionAnswer.QuizID != 0 && _newQuizQuestionAnswer.AnswerID != 0) { _listOfQuizQuestionAnswers.Add(_newQuizQuestionAnswer); } } return(_listOfQuizQuestionAnswers); }