Grade Does Not Increment!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mercea
    New Member
    • Aug 2007
    • 35

    Grade Does Not Increment!

    Hi all,
    i have a web page which contains a gridview. On this gridview, the user uses a set of radiobuttonlist s to select an option. the selected option is then to be compared with the data in a table. I draw out this data using a dataset table adapter. i want to compare the two values and if they are the same, increment Grade by 1. This is my code below. When i run it, grade always remains at zero. Can you please help me check why its not working? thanks
    Code:
     string studentAnswer;
        string lecturerAnswer;
        int Grade = 0;
        int i = 1;
              
          public void Button1_Click(object sender, EventArgs e)
        {
            DataSet1TableAdapters.QuestionsTTableAdapter adapter = new DataSet1TableAdapters.QuestionsTTableAdapter();
            DataSet1.QuestionsTDataTable questions = adapter.GetData();
            DataSet1.QuestionsTRow rowT = questions[i];
            
            
            foreach ( GridViewRow row in GridView1.Rows ) 
            {
                lecturerAnswer = rowT.Answers;
                i++;
                RadioButtonList radiolist = (RadioButtonList)row.FindControl("radlist");
                
                if (radiolist != null  )
                {
                    foreach (ListItem litem in radiolist.Items)
                    {
                        if (litem.Selected)
                        {
                            studentAnswer = radiolist.SelectedValue.ToString();
                        }     
                    }
                }
                if (studentAnswer == lecturerAnswer)
                {
                    Grade++;
                }
                else
                {
                    Grade= Grade + 0;
                }
            }
            Label1.Text = "You have Scored " + Grade;
        }
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Originally posted by mercea
    Hi all,
    i have a web page which contains a gridview. On this gridview, the user uses a set of radiobuttonlist s to select an option. the selected option is then to be compared with the data in a table. I draw out this data using a dataset table adapter. i want to compare the two values and if they are the same, increment Grade by 1. This is my code below. When i run it, grade always remains at zero. Can you please help me check why its not working? thanks
    Code:
     string studentAnswer;
        string lecturerAnswer;
        int Grade = 0;
        int i = 1;
              
          public void Button1_Click(object sender, EventArgs e)
        {
            DataSet1TableAdapters.QuestionsTTableAdapter adapter = new DataSet1TableAdapters.QuestionsTTableAdapter();
            DataSet1.QuestionsTDataTable questions = adapter.GetData();
            DataSet1.QuestionsTRow rowT = questions[i];
            
            
            foreach ( GridViewRow row in GridView1.Rows ) 
            {
                lecturerAnswer = rowT.Answers;
                i++;
                RadioButtonList radiolist = (RadioButtonList)row.FindControl("radlist");
                
                if (radiolist != null  )
                {
                    foreach (ListItem litem in radiolist.Items)
                    {
                        if (litem.Selected)
                        {
                            studentAnswer = radiolist.SelectedValue.ToString();
                        }                   
                     }
                     if (studentAnswer == lecturerAnswer)
                        {
                            Grade++;
                         }
                         else
                         {
                             Grade= Grade + 0;
                          } 
                }
                      }
            Label1.Text = "You have Scored " + Grade;
        }
    See bolded code above. I think the reason that Grade was not being incremented is because the only studentAnswer being compared was the very last studentAnswer in radiolist.Items .

    Place this code
    Code:
    if (studentAnswer == lecturerAnswer)
                        {
                            Grade++;
                         }
                         else
                         {
                             Grade= Grade + 0;
                          }
    after this conditional statement.
    Code:
     if (litem.Selected)
                        {
                            studentAnswer = radiolist.SelectedValue.ToString();
                        }
    Nathan

    Comment

    Working...