I am taking a column from a table and comparing each row in that table with a similar column in a gridview row by row. For each identical answer, grade increments by 1. after all the rows have been iterated through, the final grade is placed in another table. This worked for a while till i inserted in cookie variables for the updating it now works on and off. have any ideas what could be causing that? sometimes it increments the grade and updates the table rightly and at other times on postback, it just reloads the page! could the cookie variables be causing a problem?
here's my code:
Expand|Select|Wrap|Line Numbers
- protected void Page_Load(object sender, EventArgs e)
- {
- TextBox1.Text = (string)Session["test_course"];
- }
- //Declaring the variables
- string studentAnswer;
- string lecturerAnswer;
- int Grade;
- int i = 0;
- int j = 0;
- public void Button1_Click(object sender, EventArgs e)
- {
- //Code to retrieve data from Questions table
- DataSet1TableAdapters.QuestionsTTableAdapter adapter = new DataSet1TableAdapters.QuestionsTTableAdapter();
- DataSet1.QuestionsTDataTable questions = adapter.GetData();
- //Code to retrieve data from test101 table
- DataSet1TableAdapters.test101TableAdapter testadapter = new DataSet1TableAdapters.test101TableAdapter();
- DataSet1.test101DataTable grading = testadapter.GetData();
- //looping through each row in gidview to retrieve student's answer to multiple choice questions
- foreach ( GridViewRow row in GridView1.Rows )
- {
- //looping through questions table via table adapter to retrieve the lecturer's answer
- foreach(DataRow rowss in questions.Rows)
- {
- DataSet1.QuestionsTRow rowT = questions[i];
- if (TextBox1.Text == rowT.coursecode)
- {
- lecturerAnswer = rowT.Answers;
- break;
- }
- else
- {
- i++;
- }
- }
- i++;
- RadioButtonList radiolist = (RadioButtonList)row.FindControl("RadioButtonList1");
- if (radiolist != null)
- {
- foreach (ListItem litem in radiolist.Items)
- {
- if (litem.Selected)
- {
- studentAnswer = radiolist.SelectedItem.Value.ToString();
- }
- }
- }
- //Grading
- if (studentAnswer == lecturerAnswer)
- {
- Grade = Grade + 1;
- }
- else
- {
- Grade= Grade + 0;
- }
- }
- //storing grade in session variable so that grade is not lost on post back
- Session["grade"] = Grade;
- if (Session["grade"] == null)
- {
- Grade = 0;
- }
- else
- {
- Grade = (int)Session["grade"];
- }
- //use of cookies to upgrade student's data. get student details, find particular row that matches and update that row.
- HttpCookie cookie = Request.Cookies["userdata"];
- foreach(DataRow rows in grading.Rows)
- {
- DataSet1.test101Row rowG = grading[j];
- if (rowG.Course_code == cookie["course"] && rowG.Registration == cookie["reg"] && rowG.Surname == cookie["surname"])
- {
- rowG.BeginEdit();
- rowG.Grade = Grade;
- rowG.EndEdit();
- testadapter.Update(grading);
- grading.AcceptChanges();
- Response.Redirect("Direction3.aspx");
- }
- else { j++; }
- }
- }
- thanks....