public void UpdateOutput(Participant model) { using (QuizEntities db = new QuizEntities()) { db.Entry(model).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } }
public Participant Insert(Participant model) { using (QuizEntities db = new QuizEntities()) { db.Participants.Add(model); db.SaveChanges(); return(model); } }
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e) { string createdUsername = CreateUserWizard1.UserName; string createdPassword = CreateUserWizard1.Password; //dynamic profile = ProfileBase.Create(createdUsername); CreateUserWizardStep step1 = (CreateUserWizardStep)CreateUserWizard1.FindControl("Step1"); //DropDownList k1 = (DropDownList)step1.ContentTemplateContainer.FindControl("ddlDept"); DropDownList k2 = (DropDownList)step1.ContentTemplateContainer.FindControl("ddlRole"); TextBox k3 = (TextBox)step1.ContentTemplateContainer.FindControl("tbxName"); TextBox k4 = (TextBox)step1.ContentTemplateContainer.FindControl("UserName"); TextBox tbxEmail = (TextBox)step1.ContentTemplateContainer.FindControl("tbxEmail"); Roles.AddUserToRole(createdUsername, k2.Text); // string encryptedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(createdPassword, "MD5"); //try //{ QHSEQuiz.Model.Hub hub = new QHSEQuiz.Model.Hub(); hub.Name = k3.Text; hub.UserName = createdUsername; //.Password = encryptedPassword; if (tbxEmail.Text != "") { hub.Email = tbxEmail.Text; } else { hub.Email = "N/A"; } hub.Role = k2.SelectedItem.Text; context.Hubs.Add(hub); context.SaveChanges(); //} //catch (DbEntityValidationException dbEx) //{ // foreach (var validationErrors in dbEx.EntityValidationErrors) // { // foreach (var validationError in validationErrors.ValidationErrors) // { // System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", // validationError.PropertyName, // validationError.ErrorMessage); // } // } //} }
protected void btnSubmit3choiceAnswer_Click(object sender, EventArgs e) { string answer = ddl3choiceAnswer.Text; try { Question q = new Question(); q.OrderInQuiz = Convert.ToInt32(tbxOrder.Text); q.QuizId = Convert.ToInt32(Session["QuizId"]); q.Weighted = Convert.ToDecimal(tbxWeighted.Text); q.Type = ddlType.SelectedValue; q.Description = tbx3choiceQuestion.Text; context.Questions.Add(q); context.SaveChanges(); foreach (string choice in choiceList) { QuestionOption qo = new QuestionOption(); qo.QuestionId = q.QuestionId; qo.Description = choice; context.QuestionOptions.Add(qo); context.SaveChanges(); } QuestionAnswer qa = new QuestionAnswer(); qa.QuestionId = q.QuestionId; qa.OptionId = context.QuestionOptions.Where(x => x.Description == answer).Where(x => x.QuestionId == q.QuestionId).Select(x => x.OptionId).First(); context.QuestionAnswers.Add(qa); context.SaveChanges(); Response.Redirect("~/Admin/ManageQuestions.aspx"); } catch { lblError.Text = "Error saving data!"; } }
/// <summary> /// Save a single user answer to the db /// </summary> /// <param name="data"></param> public void SaveAnswer(QuizResponse data) { using (QuizEntities context = new QuizEntities()) { context.QuizResponses.AddObject(new QuizResponse { Date = DateTime.Now, AnswerId = data.AnswerId, QuestionId = data.QuestionId }); context.SaveChanges(); } }
protected void gvQuestions_RowDeleting(object sender, GridViewDeleteEventArgs e) { try { int qId = Convert.ToInt32(gvQuestions.DataKeys[e.RowIndex].Values[0]); using (QuizEntities context = new QuizEntities()) { Question question = (from x in context.Questions where x.QuestionId == qId select x).FirstOrDefault(); context.Questions.Remove(question); context.SaveChanges(); } this.BindGrid(); } catch { Response.Redirect("~/Admin/QuizList.aspx"); } }
protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e) { string username = gvUser.Rows[e.RowIndex].Cells[1].Text; string query = "SELECT DISTINCT UserId FROM aspnet_Users WHERE UserName = '******'"; con = new SqlConnection(conStr); SqlDataAdapter adapter = new SqlDataAdapter(query, con); DataTable dt = new DataTable(); adapter.Fill(dt); //DataSet dataset = new DataSet(); //adapter.Fill(dataset); Guid userId = Guid.Parse(dt.Rows[0][0].ToString()); using (QuizEntities context = new QuizEntities()) { QHSEQuiz.Model.Hub hub = context.Hubs.Where(x => x.UserName == username).First(); context.Hubs.Remove(hub); //aspnet_Profile profile = context.aspnet_Profile.Where(x => x.PropertyValuesString == username).First(); //context.aspnet_Profile.Remove(profile); aspnet_Users user = context.aspnet_Users.Where(x => x.UserName == username).First(); context.aspnet_Users.Remove(user); aspnet_Membership mem = context.aspnet_Membership.Where(x => x.UserId == userId).First(); context.aspnet_Membership.Remove(mem); aspnet_UsersInRoles userInRole = context.aspnet_UsersInRoles.Where(x => x.UserId == userId).First(); context.aspnet_UsersInRoles.Remove(userInRole); context.SaveChanges(); Response.Redirect("~/Admin/ManageUsers.aspx"); } }
protected void gvQuestions_RowDataBound(object sender, GridViewRowEventArgs e) { int quizResultId = Convert.ToInt32(Session["ResultId"]); if (e.Row.RowType == DataControlRowType.DataRow) { QuizControl qc = new QuizControl(); Label lblQuestionId = (Label)e.Row.FindControl("lblQuestionId"); Label lblRightWrong = (Label)e.Row.FindControl("lblRightWrong"); Label lblYourAnswer = (Label)e.Row.FindControl("lblYourAnswer"); Label lblCorrectAnswer = (Label)e.Row.FindControl("lblCorrectAnswer"); int questionId = Convert.ToInt32(lblQuestionId.Text); lblYourAnswer.Text = qc.GetOwnAnswer(questionId, quizResultId); lblCorrectAnswer.Text = qc.GetCorrectAnswer(questionId); if (lblYourAnswer.Text == lblCorrectAnswer.Text) { lblRightWrong.ForeColor = System.Drawing.Color.Green; lblRightWrong.Text = "Correct! 正确!"; correctCount++; decimal questionScore = context.Questions.Where(x => x.QuestionId == questionId).Select(x => x.Weighted).First(); weightedScore = weightedScore + questionScore; } else if (lblYourAnswer.Text == null || lblYourAnswer.Text == "") { lblYourAnswer.Visible = false; lblCorrectAnswer.Visible = false; lblRightWrong.ForeColor = System.Drawing.Color.Red; lblRightWrong.Text = "Cannot mark. This question might have been added after your quiz attempt. - 不能标记. 您的测验尝试后可能会添加此问题。"; } else { lblRightWrong.ForeColor = System.Drawing.Color.Red; lblRightWrong.Text = "Incorrect! 不正确!"; incorrectCount++; } decimal percentage = 100 * weightedScore / totalMark; lblScore.Text = percentage.ToString() + "%"; if (percentage >= 80) { lblScore.ForeColor = System.Drawing.Color.Green; } else { lblScore.ForeColor = System.Drawing.Color.Red; } QuizResult qr = context.QuizResults.Where(x => x.ResultId == quizResultId).First(); //if (qr.Correct == null || qr.Incorrect == null || qr.Result == null) //{ qr.Correct = correctCount; qr.Incorrect = incorrectCount; qr.Mark = percentage; if (percentage >= 80) { qr.Result = "Passed - 及格"; } else { qr.Result = "Failed - 不及格"; } context.SaveChanges(); //} } }
protected void btnSubmit_Click(object sender, EventArgs e) { //questionIdList = (List<int>)Session["questionIdList"]; //choiceList = (List<int>)Session["choiceList"]; if (Page.IsValid) { IIdentity id = User.Identity; dynamic profile = ProfileBase.Create(id.Name); string username = id.Name; foreach (GridViewRow row in gvQuestions.Rows) { RadioButtonList rblOptions = (RadioButtonList)row.FindControl("rblOptions"); Label lblQuestionId = (Label)row.FindControl("lblQuestionId"); Label lblQuestion = (Label)row.FindControl("lblQuestion"); if (rblOptions.SelectedItem != null) { int qId = Convert.ToInt32(lblQuestionId.Text); int choiceId = Convert.ToInt32(rblOptions.SelectedValue); questionIdList.Add(qId); choiceList.Add(choiceId); } } string confirmValue = Request.Form["confirm_value"]; if (confirmValue == "Yes") { QuizResult qr = new QuizResult(); int quizId = Convert.ToInt32(Session["QuizId"]); qr.QuizId = quizId; qr.Username = username; qr.Name = tbxName.Text; if (tbxHRMS.Text != "") { qr.HRMS = Convert.ToInt32(tbxHRMS.Text); } //qr.Name = context.Employees.Where(x => x.UserName == username).Select(x => x.Name).First(); qr.TimeSubmitted = DateTime.Now; context.QuizResults.Add(qr); context.SaveChanges(); QuizResult qr2 = context.QuizResults.Where(x => x.Username == username).Where(x => x.QuizId == quizId).OrderByDescending(x => x.ResultId).First(); int quizResultId = qr2.ResultId; for (int i = 0; i < questionIdList.Count; i++) { QuestionResponse qresp = new QuestionResponse(); qresp.ResultId = qr2.ResultId; qresp.QuestionId = questionIdList.ElementAt(i); qresp.OptionId = choiceList.ElementAt(i); qresp.TimeSubmitted = DateTime.Now; context.QuestionResponses.Add(qresp); context.SaveChanges(); } Session["ResultId"] = quizResultId; Response.Redirect("~/Hub/ViewQuizResult.aspx"); } } }
//start new quiz protected void submitquiz_click(object sender, EventArgs e) { if (Page.IsValid) { try { string name = txtname.Text.Trim(); string description = txtdescription.Text.Trim(); //string completiondesc = txtcompletiondescription.Text.Trim(); string start = txtstartdate.Text; string end = txtenddate.Text; DateTime startdate = new DateTime(); startdate = Convert.ToDateTime(start); DateTime enddate = new DateTime(); enddate = Convert.ToDateTime(end); enddate = enddate.AddHours(23).AddMinutes(59).AddSeconds(59); if ((startdate > enddate) || (startdate < updatedate)) { lblalert.Visible = true; lblalert.Text = "Please check Start date and End date!"; } else { Quiz q = new Quiz(); q.Name = name; q.Description = description; q.StartDate = startdate; q.EndDate = enddate; context.Quizs.Add(q); context.SaveChanges(); int quizId = q.QuizId; List <QHSEQuiz.Model.Hub> hubList = context.Hubs.Where(x => x.UserName.Contains("hub")).ToList(); foreach (QHSEQuiz.Model.Hub hub in hubList) { QuizDateRange dateRange = new QuizDateRange(); dateRange.HubId = hub.HubID; dateRange.QuizId = q.QuizId; dateRange.StartDate = startdate; dateRange.EndDate = enddate; context.QuizDateRanges.Add(dateRange); context.SaveChanges(); } //SqlCommand insertnew = new SqlCommand("insert into " + quizdetailstable + " (name, description, completiondescription, startdate, enddate, termsandconditions, lastupdated) values (@name, @description, @completiondescription, @startdate, @enddate, @termsandconditions, @lastupdated)"); //insertnew.Parameters.AddWithValue("name", name); //insertnew.Parameters.AddWithValue("description", description); ////insertnew.Parameters.AddWithValue("completiondescription", completiondesc); //insertnew.Parameters.AddWithValue("startdate", startdate); //insertnew.Parameters.AddWithValue("enddate", enddate); ////insertnew.Parameters.AddWithValue("termsandconditions", quizterms); ////insertnew.Parameters.AddWithValue("lastupdated", updatedate); //db insertnewquiz = new db(); //insertnewquiz.ExecuteQuery(insertnew); Session["QuizId"] = q.QuizId; Response.Redirect("~/Admin/ManageQuestions.aspx", false); } } catch { lblalert.Visible = true; lblalert.Text = "Please enter the complusory fields and try again!"; } } else { lblalert.Visible = true; lblalert.Text = "Please enter the complusory fields and try again!"; } }