Click here to Skip to main content
15,885,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On onclick event of the button , I’ve fetch the controls form the aspx page, and further I’ve used if statement in order to see which radio button is active and have store the corresponding value in the varialbe selans, using this selans, I will compare it with the value of hidden field in order to find whether checked radio button is the correct answer or not, if the answer is correct, i.e value in selans matches with the value in hidden field ( the actual answer) and the variable “count” ( initially initialized with value 0) increments accordingly, and all this code is placed in the “for loop” which will execute till the no. of controls in the GridView (you can relate it with the no. of question, as for every record GridView generates new control).

I have used variables

1) totalgrid1:- to get total score from gridview1

2)totalgrid2 :- to get total score from gridview2

3)total :- totalgrid1 + totalgrid2...it is the total score of the exam

4)correct1 :- get the number of correct answers from gridview1

5)correct2 :- get the number of correct answers from gridview2

6)correct :- correct1 + correct2.....total number of correct answers by the user

after running this program i.e attempting the exam with correct answers I am getting "score = 6" instead of getting 4 on result page because there is only 4 questions and each question carry 1 marks then how can I get 6 marks ? and also I am getting "Number of right answers = nothing is shown(it is blank)" ...it should show some value.

I can't find out where I am making mistake. Below is my code .Have a look at my code . Show me where I am making mistake and what is the solution

What I have tried:

.aspx.cs :-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{



    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
        GridView1.DataBind();


        GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
        GridView2.DataBind();

    }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}


        protected void btn_Click(object sender, EventArgs e)
      {

            RadioButton r1, r2, r3, r4;
            HiddenField hdn;
            int count = 0;
            int neg = 0;
            int total=0;
            int totalgrid1=0;
            int totalgrid2=0;

            int attempt1 = 0;
            int attempt2 = 0;
            int Tattempt = 0;
            int correct = 0;
            int correct1 = 0;
            int correct2 = 0;

            string selans = "-1";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
                r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
                r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
                r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
                hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
                if (r1.Checked)
                {
                    selans = r1.Text;

                }
                else if (r2.Checked)
                {
                    selans = r2.Text;

                }
                else if (r3.Checked)
                {
                    selans = r3.Text;

                }
                else if (r4.Checked)
                {
                    selans = r4.Text;

                }



                if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
                {
                   attempt1++;

                   if (hdn.Value == selans)
                   {
                      count++;
                       correct1++;
                   }
                    else
                    {
                      neg--;
                    }
                }

                totalgrid1 = count + neg;


            }

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
        r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
        r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
        r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
        hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
        if (r1.Checked)
        {
            selans = r1.Text;

        }
        else if (r2.Checked)
        {
            selans = r2.Text;

        }
        else if (r3.Checked)
        {
            selans = r3.Text;

        }
        else if (r4.Checked)
        {
            selans = r4.Text;

        }



        if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
        {
            attempt2++;

            if (hdn.Value == selans)
            {
                count++;
                correct2++;
            }
            else
            {
                neg--;
            }
        }

        totalgrid2 = count + neg;

    }
    total = totalgrid1 + totalgrid2;
    Tattempt = attempt1 + attempt2;
    correct = correct1 + correct2;
    Label2.Text = total.ToString();
    Label3.Text = Tattempt.ToString();
    Label4.Text = correct.ToString();

    Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns" +Label4.Text);

}


}


Result.aspx.cs :-

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Student_Result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = Request.QueryString["Score"];
    Label2.Text = Request.QueryString["AttemptedQues"];
    Label3.Text = Request.QueryString["CorrectAns"];
}
}
Posted
Updated 3-May-16 7:22am

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Use the debugger to check that everything match your expectations.
Check that GridView2 size match your expectations.
 
Share this answer
 
Comments
Member 12170781 3-May-16 13:21pm    
Thank you so much for reminding about breakpoint. Now I come to know where I am making mistake.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;


public partial class Student_Examdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        GridView1.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions");
        GridView1.DataBind();


        GridView2.DataSource = GetData("SELECT top 2 Question, Option1, Option2, Option3, Option4, CorrectAns, Explanation FROM Questions WHERE SectionId=2");
        GridView2.DataBind();

    }
}

private DataSet GetData(string query)
{
    string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;
            }
        }
    }
}


        protected void btn_Click(object sender, EventArgs e)
      {

            RadioButton r1, r2, r3, r4;
            HiddenField hdn;
            int count1 = 0;
            int count2 = 0;
            int neg1 = 0;
            int neg2 = 0;
            int total=0;
            int totalgrid1=0;
            int totalgrid2=0;

            int attempt1 = 0;
            int attempt2 = 0;
            int Tattempt = 0;
            int correct = 0;
            int correct1 = 0;
            int correct2 = 0;

            string selans = "-1";
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
                r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
                r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
                r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
                hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
                if (r1.Checked)
                {
                    selans = r1.Text;

                }
                else if (r2.Checked)
                {
                    selans = r2.Text;

                }
                else if (r3.Checked)
                {
                    selans = r3.Text;

                }
                else if (r4.Checked)
                {
                    selans = r4.Text;

                }



                if(r1.Checked || r2.Checked || r3.Checked || r4.Checked)
                {
                   attempt1++;

                   if (hdn.Value == selans)
                   {
                      count1++;
                       correct1++;
                   }
                    else
                    {
                      neg1--;
                    }
                }

                totalgrid1 = count1 + neg1;


            }

    for (int i = 0; i < GridView2.Rows.Count; i++)
    {
        r1 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad1");
        r2 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad2");
        r3 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad3");
        r4 = (RadioButton)GridView2.Rows[i].Cells[0].FindControl("rad4");
        hdn = (HiddenField)GridView2.Rows[i].Cells[0].FindControl("hf");
        if (r1.Checked)
        {
            selans = r1.Text;

        }
        else if (r2.Checked)
        {
            selans = r2.Text;

        }
        else if (r3.Checked)
        {
            selans = r3.Text;

        }
        else if (r4.Checked)
        {
            selans = r4.Text;

        }



        if (r1.Checked || r2.Checked || r3.Checked || r4.Checked)
        {
            attempt2++;

            if (hdn.Value == selans)
            {
                count2++;
                correct2++;
            }
            else
            {
                neg2--;
            }
        }

        totalgrid2 = count2 + neg2;

    }
    total = totalgrid1 + totalgrid2;
    Tattempt = attempt1 + attempt2;
    correct = correct1 + correct2;
    Label2.Text = total.ToString();
    Label3.Text = Tattempt.ToString();
    Label4.Text = correct.ToString();

    Response.Redirect("/Student/Result.aspx?Score=" + Label2.Text +"&AttemptedQues=" +Label3.Text+ "&CorrectAns=" +Label4.Text);

}


}
 
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