Click here to Skip to main content
16,020,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
heyy i am working on online examination website i want to calculate the result percentage but its only show 0.00 nothing else

What I have tried:

protected void Page_Load(object sender, EventArgs e)
{
    SqlCommand com = new SqlCommand("Select * from [Biology]", con);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt2 = new DataTable();
    da.Fill(dt2);
    grdquestions.DataSource = dt2;
    grdquestions.DataBind();  
}
protected void examsubmit_Click(object sender, EventArgs e)
{
    int correctAnswers = 0, incorrectAnswers = 0,
    totalQuestions = grdquestions.Rows.Count;
    SqlCommand com = new SqlCommand("Select * from [Biology]", con);
    SqlDataAdapter da = new SqlDataAdapter(com);
    DataTable dt = new DataTable();
    da.Fill(dt);


   for(int i = 0; i < totalQuestions; i++)
    {
      GridViewRow row = grdquestions.Rows[i];
    string answer = dt.Rows[i]["Answer"].ToString();

  var radioButtons = new List<radiobutton>
   {
    (RadioButton)row.FindControl("Option1"),
    (RadioButton)row.FindControl("Option2"),
    (RadioButton)row.FindControl("Option3"),
    (RadioButton)row.FindControl("Option4"),
    };

foreach (var radioButton in radioButtons)
{
    if (radioButton != null)
    {
        if (radioButton.Checked)
        {
            if (radioButton.Text == answer)
                correctAnswers++;
            else
            {
                incorrectAnswers++;

                radioButton.Checked = false;
            }

        }
        else if (radioButton.Text == answer)
        {
            radioButton.Checked = true;
        }
    }
}  
} 
string correctAnswerPercentage = (correctAnswers / totalQuestions).ToString("0.00%");     
label1.text= "percentage is"+correcAnswerpercentage;
}
Posted
Updated 12-Dec-18 2:12am
Comments
F-ES Sitecore 12-Dec-18 7:38am    
Use the debugger to step through your code to work out what is going on. We don't have your database, your data, your inputs etc so can't run your code for you. If you are getting 0% then correctAnswers is probably 0, the debugger will confirm or deny that. If correctAnswers is 0 then step through the code and try and work out what it isn't doing that it should.

1 solution

You're using integer division, so the result will be truncated.

Change your variables to double:
C#
double correctAnswers = 0, incorrectAnswers = 0, totalQuestions = grdquestions.Rows.Count;


EDIT: You'll also need an if (!IsPostBack) { ... } test around the data-binding code in the Page_Load method. Otherwise, the user's selections will be overwritten every time, and none of the radiobuttons will be checked by the time the button click handler runs.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlCommand com = new SqlCommand("Select * from [Biology]", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt2 = new DataTable();
        da.Fill(dt2);
        grdquestions.DataSource = dt2;
        grdquestions.DataBind();
    }
}
 
Share this answer
 
v2
Comments
AhmedHosny96 12-Dec-18 13:53pm    
after changing int to double but its not working bro ?
Richard Deeming 12-Dec-18 13:56pm    
"It's not working" isn't enough information. Are you getting an error? Incorrect output? A message from the flying spaghetti monster?

Remember, we can't see your screen. All we have to go on is what you type in this little box. Help us to help you! :)
AhmedHosny96 14-Dec-18 14:38pm    
i mean its still displaying 0.00% after changing to double
Richard Deeming 14-Dec-18 14:42pm    
OK, that would suggest it's not counting the correct answers.

What output do you get if you use:
label1.Text= string.Format("percentage is {0} ({1}/{2})", correctAnswerPercentage, correctAnswers, totalQuestions);
AhmedHosny96 14-Dec-18 14:49pm    
still its 00.0% i think its not incrementing the correct answers ?

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