Hi irshad1231,
Refer below sample.
HTML
<div>
<div id="dvQuestion" runat="server">
<h1>
<asp:Label ID="lblQuestion" runat="server" />
<asp:HiddenField runat="server" ID="hfQuestionId" />
</h1>
<asp:RadioButtonList ID="rbtnOptions" runat="server">
</asp:RadioButtonList>
<br />
<asp:Button ID="btnNext" Text="Next" runat="server" OnClick="Next" />
</div>
<div id="dvResult" runat="server" visible="false">
<h1>
<asp:Label ID="lblResult" runat="server" />
</h1>
</div>
</div>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Code
C#
private DataTable DataTableQuestions
{
get { return (DataTable)ViewState["Questions"]; }
set { ViewState["Questions"] = value; }
}
private int Score
{
get { return (int)ViewState["Score"]; }
set { ViewState["Score"] = value; }
}
static int QuestionId;
static int attemptCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Score = 0;
QuestionId = 1;
DataTableQuestions = PopulateQuestions();
GetCurrentQuestion(Convert.ToInt32(DataTableQuestions.Rows[QuestionId - 1]["QuestionId"]), PopulateQuestions());
}
}
protected void Next(object sender, EventArgs e)
{
if (QuestionId <= DataTableQuestions.Rows.Count)
{
if (rbtnOptions.SelectedIndex != -1)
{
string otionSelected = rbtnOptions.SelectedItem.Text.Trim();
string correctAnswer = CorrectAnswer(Convert.ToInt32(hfQuestionId.Value));
if (otionSelected.ToLower() != correctAnswer.ToLower())
{
attemptCount++;
}
else
{
attemptCount = 0;
}
if (attemptCount >= 1)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Answer is Wrong. Try Again')", true);
}
else
{
QuestionId++;
if (otionSelected.ToLower() == correctAnswer.ToLower())
{
Score++;
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Answer is Correct')", true);
}
GetCurrentQuestion(QuestionId, DataTableQuestions);
attemptCount = 0;
}
}
}
}
private void GetCurrentQuestion(int questionId, DataTable dtQuestions)
{
if (QuestionId <= dtQuestions.Rows.Count)
{
lblQuestion.Text = dtQuestions.Rows[questionId - 1]["QuestionDescription"].ToString();
hfQuestionId.Value = dtQuestions.Rows[questionId - 1]["QuestionId"].ToString();
DataTable dtOptions = PopulateOptions(Convert.ToInt16(hfQuestionId.Value));
List<ListItem> options = new List<ListItem>();
for (int i = 0; i < dtOptions.Rows.Count; i++)
{
options.AddRange(new ListItem[] { new ListItem(dtOptions.Rows[i][1].ToString(), i.ToString()) });
}
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(options.ToArray());
rbtnOptions.DataBind();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, QuestionId - 1);
}
}
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT QuestionId,QuestionDescription,'' AnswerSelected FROM QuestionTable ORDER BY NEWID()", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt;
}
private DataTable PopulateOptions(int questionId)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT QuestionId,Options FROM OptionTable WHERE QuestionId = @QuestionId", con))
{
cmd.Parameters.AddWithValue("@QuestionId", questionId);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt;
}
private string CorrectAnswer(int questionId)
{
string answer = "";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Answer FROM AnswerTable WHERE QuestionId = @QuestionId", con))
{
cmd.Parameters.AddWithValue("@QuestionId", questionId);
con.Open();
answer = cmd.ExecuteScalar().ToString();
con.Close();
}
}
return answer;
}