protected void Page_Load(object sender, EventArgs e) { ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); var chaps = x.Questions.Select(p => new { ChapterID = p.ChapterID, QuestionID = p.QuestionID, QuestionType = p.QuestionBlank != null ? "Blank" : p.QuestionBool != null ? "Bool" : "MCQ", Question = p }).GroupBy(p => new { p.ChapterID, p.QuestionType }); List<Object > list = new List<Object>(); foreach (var t in chaps) { /* list.Add(new { ChapterID=t.Key.ChapterID, QuestionType=t.Key.QuestionType, Count = t.Count() });*/ foreach (var k in t.OrderBy(p=>p.QuestionID*rnd.Next()).Take(3)) { list.Add(k); } } GridView1.DataSource = list; //chaps.ToList(); GridView1.DataBind(); }
protected void Button1_Click(object sender, EventArgs e) { ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); int index = int.Parse(RadioButtonList1.SelectedValue); ExamDbModel.Question que=new ExamDbModel.Question(); que.QuestionText= txtQuestion.Text; que.ChapterID= int.Parse(txtChapter.SelectedValue); x.AddToQuestions(que); x.SaveChanges(); switch (index) { case 0: //Blank ExamDbModel.QuestionBlank blank = new ExamDbModel.QuestionBlank(); blank.CorrectAnswer = BlankAns.Text; blank.QuestionID = que.QuestionID; x.AddToQuestionBlanks(blank); x.SaveChanges(); Label1.Text = "BlankRequestion is added"; //Fill the grid BindBlanks(); break; case 1: //Bool ExamDbModel.QuestionBool boool = new ExamDbModel.QuestionBool(); boool.QuestionID = que.QuestionID; boool.Answer1 = BoolAns1.Text; boool.Answer2 = BoolAns2.Text; boool.CorrectAnswer = BoolAns.Checked; x.AddToQuestionBools(boool); x.SaveChanges(); Label1.Text = "BoolQequestion is added"; BindBool(); break; case 2: //MCQ ExamDbModel.QuestionMcq mcq = new ExamDbModel.QuestionMcq(); mcq.QuestionID = que.QuestionID; mcq.Answer1 = McaA1.Text; mcq.Answer2 = McqA2.Text; mcq.Answer3 = McqA3.Text; mcq.Answer4 = McqA4.Text; mcq.CorrectAnswer = McqCorrect.Text; x.AddToQuestionMcqs(mcq); x.SaveChanges(); Label1.Text = "MCQQuestion is added"; BindMCQ(); break; } }
//Blanks public void BindBlanks() { int chapId = int.Parse(txtChapter.SelectedValue); ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); var result = x.Questions.Where(p => p.ChapterID == chapId).Select(p => new { QuestionID=p.QuestionID, Text= p.QuestionText, CorrectAnswer=p.QuestionBlank.CorrectAnswer }).Where(k=> !string.IsNullOrEmpty(k.CorrectAnswer)); GridView1.DataSource = result.ToList(); GridView1.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); var chaps = x.Questions.Select(p => new { ChapterID = p.ChapterID, QuestionID = p.QuestionID, QuestionType = p.QuestionBlank != null ? "Blank" : p.QuestionBool != null ? "Bool" : "MCQ", Question = p }).GroupBy(p => new { p.ChapterID, p.QuestionType }); List<RndQue> list = new List<RndQue>(); foreach (var t in chaps) { foreach (var k in t.OrderBy(p => p.QuestionID * rnd.Next()).Take(2)) { list.Add(new RndQue { ChapterID = k.ChapterID.Value, QuestionID = k.QuestionID, QuestionType = k.QuestionType, Question=k.Question, CorrectAnswer = k.QuestionType=="Blank" ? k.Question.QuestionBlank.CorrectAnswer : k.QuestionType=="Bool" ? k.Question.QuestionBool.CorrectAnswer.Value.ToString() : k.Question.QuestionMcq.CorrectAnswer }); } } GridView1.DataSource = list; GridView1.DataBind(); Session["list"] = list; ShowRecord(); } }
protected void txtSubject_SelectedIndexChanged(object sender, EventArgs e) { ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); int subjectId=int.Parse(txtSubject.SelectedValue); var result = x.Chapters.Where(p => p.SubjectID == subjectId).ToList(); txtChapter.DataSource = result; txtChapter.DataTextField = "ChapterName"; txtChapter.DataValueField = "ChapterID"; txtChapter.DataBind(); txtChapter.Items.Insert(0, new ListItem("select", "0")); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ExamDbModel.ExamDbEntities x = new ExamDbModel.ExamDbEntities(); txtSubject.DataSource = x.Subjects; txtSubject.DataTextField = "SubjectName"; txtSubject.DataValueField = "SubjectID"; txtSubject.DataBind(); txtSubject.Items.Insert(0, new ListItem("select", "0")); } }