Click here to Skip to main content
15,991,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
iam working on online examination website project in asp.net my stack is that i want to display final score and exam percentage i have done enough to display this but its showing final score = 0 and percentage = 0 till now here is my code please help me.... thank u

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)
    {
        SqlCommand com = new SqlCommand("Select * from [Biology]", con);
        SqlDataAdapter da = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        da.Fill(dt);

    int correctAnswers = 0, incorrectAnswers = 0,
    totalMarks = 0, totalQuestions = grdquestions.Rows.Count;

  for (int i = 0; i < totalQuestions; i++)
    {
    GridViewRow row = grdquestions.Rows[i];
    string answer = dt.Rows[i]["Answer"].ToString();
    int marks = Convert.ToInt32(dt.Rows[i]["Marks"]);
    var radioButtons = new List<radiobutton>
    {
        (RadioButton)row.FindControl("rdOption1"),
        (RadioButton)row.FindControl("rdOption2"),
        (RadioButton)row.FindControl("rdOption3"),
        (RadioButton)row.FindControl("rdOption4"),
    };

    foreach (var radioButton in radioButtons)
    {
        if (radioButton.Checked)
        {
            if (radioButton.Text == dt.Rows[i]["Answer"].ToString())
            {
                correctAnswers++;
                totalMarks += marks;
            }
            else
            {
                incorrectAnswers++;
                radioButton.Checked = false;
            }
        }
        else if (radioButton.Text == dt.Rows[i]["Answer"].ToString())
        {
            correctAnswers++;
            radioButton.Checked = true;
            
        }
    }
}

Label2.Text = "FinalScore is :" + totalMarks;

string correctAnswerPercentage = (correctAnswers / totalQuestions).ToString("0.00%");
Label2.Text = "Percentage is "+correctAnswerPercentage;
    }
}
Posted
Updated 14-Dec-18 8:12am
Comments
Richard Deeming 14-Dec-18 14:23pm    
REPOST
You have already posted this:
https://www.codeproject.com/Questions/1271553/Calculating-percentage-and-score-in-ASP-NET-online[^]

The answer has not changed in the last two days!

1 solution

Because correctAnswers and totalQuestions are integers, correctAnswers / totalQuestions will also return an integer value. And since correctAnswers can never be greater than totalQuestions the division can only ever return 0 or 1!

You could try doing this:
C#

string correctAnswerPercentage = (correctAnswers * 100 / totalQuestions).ToString("00%");Or this:
C#

string correctAnswerPercentage = ((float)correctAnswers * 100.0 / (float) totalQuestions).ToString("00.0%");
 
Share this answer
 
Comments
Richard Deeming 14-Dec-18 14:25pm    
Indeed - as I told him on Wednesday[^].

Prepare for the inevitable "it's not working" comment, followed by two days of silence, and yet another repost of the same question.
AhmedHosny96 14-Dec-18 14:28pm    
brother iam frustrated at this topic i have done all the tips u have given but nothing is working help me please...?
Richard Deeming 14-Dec-18 14:33pm    
You had two options:

1) Reply to my comment asking for more information;
2) Ignore my comment, and post a new copy of the question;

Which one did you go for?

The code in this new copy of your question shows that you didn't even try the solution I posted.

If you don't understand what Griff and I have now both told you, then ask us to explain. Don't just say "not working" and walk away from the conversation.
AhmedHosny96 14-Dec-18 14:36pm    
ok sorry for this bro <3
Richard Deeming 14-Dec-18 14:37pm    
No problem. :)

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