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 :-
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 :-
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"];
}
}