Click here to Skip to main content
15,887,280 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Am using the following code for calculation score of question paper containing multiple choice question. But am facing problem that,the score is correct when all the questions of the paper is answered. Suppose if one doesn't ans any of the question then its showing wrong score. Want that score is updated for each correct answer.
C#
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        string selans = "-1";
        int Score = 0;
        foreach (GridViewRow row in GridView1.Rows)
        {
            RadioButton r1 = ((RadioButton)row.FindControl("rad1"));
            RadioButton r2 = ((RadioButton)row.FindControl("rad2"));
            RadioButton r3 = ((RadioButton)row.FindControl("rad3"));
            RadioButton r4 = ((RadioButton)row.FindControl("rad4"));
            HiddenField hdn = ((HiddenField)row.FindControl("hf"));
            if (r1.Checked)
            {
                selans = "1";
            }
            else if (r2.Checked)
            {
                selans = "2";
            }
            else if (r3.Checked)
            {
                selans = "3";
            }
            else if (r4.Checked)
            {
                selans = "4";
            }
            if (selans == hdn.Value)
            {
                Score = Score + 1;
                com = new SqlCommand("update Exam set Score='" + Score + "' where PCode='" + Label14.Text + "'", con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
            }
        }
    }
Posted
Updated 7-Oct-17 6:16am
Comments
george4986 13-Sep-14 2:52am    
whats the value of hdn.Value?
why dont u add another else after else if (r4.Checked){}
and update score as zero?
Mishti Choudhary 13-Sep-14 3:21am    
Tried but same result. The problem is with the for loop that its generating correct score only if all the questions of the question set is answered. If even any one of the ans is not answered its giving wrong score. Is it possible that for each row the score gets updated indivisually instead of all the questions altogether?
Mishti Choudhary 13-Sep-14 3:26am    
Hidden field is scoring correct ans of indivisual questions in the same row with the options,in a gridview.
Mishti Choudhary 13-Sep-14 3:30am    
I tried the same thing using paging property of gridview in which gridview was having one question per page. Then was getting correct score but with that problem was changing page on button click. Wasn't able to change the Page of Gridview on button click. Had to select next page manually each time. So tried this alternative.
george4986 13-Sep-14 4:29am    
plz remove for each loop and add below code
RadioButton rd = (RadioButton)sender;
GridViewRow row = (GridViewRow)rd.NamingContainer;

1 solution

I think you're following the wrong approach here. Your data is probably coming from a datasource. That datasource probably contains the question, the answers options, and the correct answer. The logic to determine the score per question, or the score for the entire test, belongs in your business object, not in your GUI layer. I would advice you to do the following:

- Create a Question class, looking something like this (you can change your answers to be a collection if needed, but I wanted to keep the example simple
C#
public class Question
{
  public string Question { get; set; }

  public string AnswerOne { get; set; }

  public string AnswerTwo { get; set; }

  public string AnswerThree { get; set; }

  public string AnswerFour { get; set; }

  public int CorrectAnswer { get; set; }

  public bool AnswerOneSelected { get; set; }

  public bool AnswerTwoSelected { get; set; }

  public bool AnswerThreeSelected { get; set; }

  public bool AnswerFourSelected { get; set; }

  public bool IsAnswerCorrect
  {
    get
    {
      bool[] currentAnswers = new bool[] { AnswerOneSelected, AnswerTwoSelected, AnswerThreeSelected, AnswerFourSelected };
      return currentAnswers[CorrectAnswer - 1];
    }
  }

  // You can use these properties if your datasource contains different scores per question
  public int ScoreForCorrectAnswer { get; set; }

  public int Score
  {
    get
    {
      return IsAnswerCorrect ? ScoreForCorrectAnswer : 0;
    }
  }
}

Bind a list of these objects to your GridView, binding each Answer...Selected property to each of the Radiobuttons.

Next, you can either implement INotifyPropertyChanged on your objects to know when a different answer is selected, or continue to use the CheckedChanged event that you are already using.

You can get the score for the test by querying your collection of objects like this:
C#
var score = questions.Where(q => q.IsAnswerCorrect).Count();

If you want to use a different scoring mechanism, use the Score property instead, and do something like:
C#
var score = questions.Sum(q => q.Score);
 
Share this answer
 
Comments
Mishti Choudhary 19-Sep-14 5:13am    
Thank you. I got it solved. It was solved by taking button inside the gridview.
Member 13658833 5-Feb-18 4:37am    
hi friend i'm also develop same logic but i don't know how to get selected radio button value, i'm using repeater control in front end page by reading data dynamically
cloud you please help me this out
please explain how you implement this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900