I am using a gridview for generating question with its four options. Calculating the score by comparing the correct ans from the table. But this operation is successfully executing for indivisual pages,when moving to next page,operation of calculating score is taking place but only for the question on that particular page,its not calculating the ultimate score i.e summing up to the score previously obtained. Along with carrying and summing up the result of one page I want that on the same button click new page of the gridview autogenerates without using the paging property of the gridview. My Code So far: C# Code Behind:
protected void btn_Click(object sender, EventArgs e)
{
string selans = "-1";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
RadioButton r1 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad1");
RadioButton r2 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad2");
RadioButton r3 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad3");
RadioButton r4 = (RadioButton)GridView1.Rows[i].Cells[0].FindControl("rad4");
HiddenField hdn = (HiddenField)GridView1.Rows[i].Cells[0].FindControl("hf");
if (r1.Checked)
{
selans = "1";
}
else if (r2.Checked)
{
selans = "2";
}
else if (r3.Checked)
{
selans = "3";
}
else if (r4.Checked)
{
selans = "4";
}
if (hdn.Value==selans)
{
score = score + 1;
com = new SqlCommand("update Exam set Score='"+score+"' where PCode='"+ Label14.Text+"'", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
else
{
}
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
private void GetData()
{
str = ("select Branch_Code from Branches where Branch_Name='" + Label1.Text + "'");
con.Open();
com = new SqlCommand(str, con);
count = Convert.ToInt16(com.ExecuteScalar());
con.Close();
con.Open();
SqlDataAdapter da1 = new SqlDataAdapter("select Question,opt1,opt2,opt3,opt4,c_ans from Qbank where Branch_Code='" + count + "'", con);
DataSet ds = new DataSet();
da1.Fill(ds, "Qbank");
DataTable dt = ds.Tables[0];
Session["da"] = da1;
Session["dt"] = dt;
con.Close();
BindData();
}
private void BindData()
{
GridView1.DataSource = (DataTable)Session["dt"];
GridView1.DataBind();
}