Click here to Skip to main content
15,998,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey everyone i am on online examination website project in asp.net i want to calculate the percentage , to display the no of correct answers , and the final score by(multiplying [marks] and no of correct answers) here is my code it only displays correct answers not the incorrect answers


THANK YOU!

What I have tried:

C#
protected void examsubmit_Click(object sender, EventArgs e) {

    DataTable dt = new DataTable();
    string sqlStatement = "SELECT * FROM [Biology]";



      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString2"].ConnectionString);

        using (SqlCommand cmd = new SqlCommand(sqlStatement, con))
        {
            cmd.CommandType = CommandType.Text;
            using (SqlDataAdapter da = new SqlDataAdapter(cmd))
            {
                da.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    int index = 0;
                    foreach (GridViewRow row in grdquestions.Rows)
                    {
                        RadioButton rb1 = (RadioButton)row.FindControl("Option1");
                        RadioButton rb2 = (RadioButton)row.FindControl("Option2");
                        RadioButton rb3 = (RadioButton)row.FindControl("Option3");
                        RadioButton rb4 = (RadioButton)row.FindControl("Option4");
                        //
                        int marks = Convert.ToInt32(dt.Rows[index]["Marks"]);
                        if (rb1 != null || rb2 != null || rb3 != null || rb4 != null)

                            if (rb1.Text == dt.Rows[index]["Answer"].ToString())
                            {
                                //do something if matched

                                rb1.Checked = true;


                            }

                            else if (rb2.Text == dt.Rows[index]["Answer"].ToString())
                            {
                                //do something if matched

                                rb2.Checked = true;


                            }

                            else if (rb3.Text == dt.Rows[index]["Answer"].ToString())
                            {
                                //do something if matched

                                rb3.Checked = true;


                            }

                            else if (rb4.Text == dt.Rows[index]["Answer"].ToString())
                            {
                                //do something if matched

                                rb4.Checked = true;


                            }
                                index++;


                        int total =index * marks;
                        Label2.Text = "FinalScore is :" + total;


                          }


                        }

                    }

                }


            }
Posted
Updated 11-Dec-18 21:46pm
v2

1 solution

That's because you have only told it to display the total: i.e. index * marks
And you are setting that inside a loop, so it will only ever show the last rows worth of data.

If you want to to show other information, you have to add code to do that! (And probably do it outside the loop which checks all of the answers...)
 
Share this answer
 

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