Dear All,
I would like to to get the correct total values of correct, wrong answer from the below code.
it is a quiz with Next button and Submit button on last question:
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" />
<asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" />
<asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" />
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Next" />
<asp:Button ID="btnSkip" runat="server" OnClick="btnSkip_Click" Text="Skip" /></td>
<asp:Label ID="lblQuizResult" runat="server" BorderStyle="None" Style="font-weight: 700;"></asp:Label>
<asp:Label ID="lblgoodAnswer" runat="server" BorderStyle="None" Font-Bold="true"></asp:Label>
<asp:Label ID="lblwrongAnswer" runat="server" BorderStyle="None" Font-Bold="true"></asp:Label>
<asp:Label ID="lblskipAnswer" runat="server" BorderStyle="None" Font-Bold="true"></asp:Label>
int index = 0;
int goodAnswer = 0;
int wrongAnswer = 0;
int skipAnswer = 0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionQuiz"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd;
string query1 = "SELECT COUNT(SubjectID) FROM EXAMQuestion WHERE (SubjectID ='" + subid + "') AND (CategoryID ='" + 1 + "')";
cmd = new SqlCommand(query1, con);
con.Open();
int totaQuestions = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
string sql = "SELECT * FROM EXAMQuestion WHERE SubjectID ='" + subid + "'";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds, "EXAMQuestion");
if (ds.Tables[0].Rows.Count > 0)
{
RadioButton1.Enabled = true;
RadioButton2.Enabled = true;
RadioButton3.Enabled = true;
RadioButton4.Enabled = true;
if (btnSubmit.Text == "Next" && index < totaQuestions)
{
string user_answer = "";
if (RadioButton1.Checked == true)
{
user_answer = "Option1";
}
else if (RadioButton2.Checked == true)
{
user_answer = "Option2";
}
else if (RadioButton3.Checked == true)
{
user_answer = "Option3";
}
else if (RadioButton4.Checked == true)
{
user_answer = "Option4";
}
string correct_answer = ds.Tables[0].Rows[index]["CorrectAnswer"].ToString();
if (user_answer == "correct_answer")
{
goodAnswer++;
}
else
{
wrongAnswer++;
}
index++;
lblQuestion.Text = "Question " + (index + 1).ToString() + ": " + ds.Tables[0].Rows[index]["QuestionName"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[index]["Option1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[index]["Option2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[index]["Option3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[index]["Option4"].ToString();
// Radio buttons need to be initialized:
RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
btnSkip.Enabled = true;
if (index == (totaQuestions - 1))
{
btnSubmit.Text = "Submit";
}
}
else
{
string user_answer = "";
if (RadioButton1.Checked == true)
{
user_answer = "Option1";
}
else if (RadioButton2.Checked == true)
{
user_answer = "Option2";
}
else if (RadioButton3.Checked == true)
{
user_answer = "Option3";
}
else if (RadioButton4.Checked == true)
{
user_answer = "Option4";
}
string correct_answer = ds.Tables[0].Rows[index]["CorrectAnswer"].ToString();
if (user_answer == "correct_answer")
{
goodAnswer++;
}
else
{
wrongAnswer++;
}
lblgoodAnswer.ForeColor = System.Drawing.ColorTranslator.FromHtml("#1daf05");
lblwrongAnswer.ForeColor = System.Drawing.ColorTranslator.FromHtml("#cf014a");
lblskipAnswer.ForeColor = System.Drawing.ColorTranslator.FromHtml("#4035d3");
lblQuizResult.Text = "Quiz Result :";
lblgoodAnswer.Text = "Correct Answer = " + goodAnswer.ToString();
lblwrongAnswer.Text = "Incorrect Answer = " + wrongAnswer.ToString();
lblskipAnswer.Text = "Skipped Question = " + skipAnswer.ToString();
btnSubmit.Enabled = false;
index = 0;
goodAnswer = 0;
skipAnswer = 0;
wrongAnswer = 0;
}
}
}
Many thanks in advance