Hi irshad1231,
Refer below sample.
HTML
<asp:Label ID="Label1" runat="server"></asp:Label>
of 30
<div id="dvQuestion" runat="server">
<h1>
<asp:Label ID="lblQuestion" runat="server" />
<asp:HiddenField runat="server" ID="hfQuestionId" />
<asp:HiddenField runat="server" ID="hfQuestiondesc" />
</h1>
<asp:RadioButtonList ID="rbtnOptions" runat="server" CausesValidation="true" ViewStateMode="Enabled">
</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>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
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; }
}
private int Count
{
get { return (int)ViewState["Count"]; }
set { ViewState["Count"] = value; }
}
private int QuestionId
{
get { return (int)ViewState["QuestionId"]; }
set { ViewState["QuestionId"] = value; }
}
static int attemptCount = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Score = 0;
QuestionId = 1;
Count = 1;
DataTableQuestions = PopulateQuestions();
GetCurrentQuestion(DataTableQuestions);
}
Label1.Text = "Q" + Count.ToString();
}
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++;
Count++;
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Answer is Correct')", true);
}
GetCurrentQuestion(DataTableQuestions);
attemptCount = 0;
}
}
}
}
private void GetCurrentQuestion(DataTable dtQuestions)
{
if (QuestionId <= dtQuestions.Rows.Count)
{
lblQuestion.Text = dtQuestions.Rows[QuestionId - 1]["QuestionDescription"].ToString();
hfQuestiondesc.Value = lblQuestion.Text;
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();
Label1.Text = "Q" + Count.ToString();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, QuestionId - 1);
Label1.Text = "Q" + (Count - 1).ToString();
}
}
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;
}
VB.Net
Private Property DataTableQuestions As DataTable
Get
Return CType(ViewState("Questions"), DataTable)
End Get
Set(ByVal value As DataTable)
ViewState("Questions") = value
End Set
End Property
Private Property Score As Integer
Get
Return CInt(ViewState("Score"))
End Get
Set(ByVal value As Integer)
ViewState("Score") = value
End Set
End Property
Private Property Count As Integer
Get
Return CInt(ViewState("Count"))
End Get
Set(ByVal value As Integer)
ViewState("Count") = value
End Set
End Property
Private Property QuestionId As Integer
Get
Return CInt(ViewState("QuestionId"))
End Get
Set(ByVal value As Integer)
ViewState("QuestionId") = value
End Set
End Property
Shared attemptCount As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Score = 0
QuestionId = 1
Count = 1
DataTableQuestions = PopulateQuestions()
GetCurrentQuestion(DataTableQuestions)
End If
Label1.Text = "Q" & Count.ToString()
End Sub
Protected Sub [Next](ByVal sender As Object, ByVal e As EventArgs)
If QuestionId <= DataTableQuestions.Rows.Count Then
If rbtnOptions.SelectedIndex <> -1 Then
Dim otionSelected As String = rbtnOptions.SelectedItem.Text.Trim()
Dim correctAnswer As String = correctAnswer(Convert.ToInt32(hfQuestionId.Value))
If otionSelected.ToLower() <> correctAnswer.ToLower() Then
attemptCount += 1
Else
attemptCount = 0
End If
If attemptCount >= 1 Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Answer is Wrong. Try Again')", True)
Else
QuestionId += 1
If otionSelected.ToLower() = correctAnswer.ToLower() Then
Score += 1
Count += 1
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Answer is Correct')", True)
End If
GetCurrentQuestion(DataTableQuestions)
attemptCount = 0
End If
End If
End If
End Sub
Private Sub GetCurrentQuestion(ByVal dtQuestions As DataTable)
If QuestionId <= dtQuestions.Rows.Count Then
lblQuestion.Text = dtQuestions.Rows(QuestionId - 1)("QuestionDescription").ToString()
hfQuestiondesc.Value = lblQuestion.Text
hfQuestionId.Value = dtQuestions.Rows(QuestionId - 1)("QuestionId").ToString()
Dim dtOptions As DataTable = PopulateOptions(Convert.ToInt16(hfQuestionId.Value))
Dim options As List(Of ListItem) = New List(Of ListItem)()
For i As Integer = 0 To dtOptions.Rows.Count - 1
options.AddRange(New ListItem() {New ListItem(dtOptions.Rows(i)(1).ToString(), i.ToString())})
Next
rbtnOptions.Items.Clear()
rbtnOptions.Items.AddRange(options.ToArray())
rbtnOptions.DataBind()
Label1.Text = "Q" & Count.ToString()
Else
dvQuestion.Visible = False
dvResult.Visible = True
lblResult.Text = String.Format("You Scored {0}/{1}", Score, QuestionId - 1)
Label1.Text = "Q" & (Count - 1).ToString()
End If
End Sub
Private Function PopulateQuestions() As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT QuestionId,QuestionDescription,'' AnswerSelected FROM QuestionTable ORDER BY NEWID()", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
con.Open()
da.Fill(dt)
con.Close()
End Using
End Using
End Using
Return dt
End Function
Private Function PopulateOptions(ByVal questionId As Integer) As DataTable
Dim dt As DataTable = New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT QuestionId,Options FROM OptionTable WHERE QuestionId = @QuestionId", con)
cmd.Parameters.AddWithValue("@QuestionId", questionId)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
con.Open()
da.Fill(dt)
con.Close()
End Using
End Using
End Using
Return dt
End Function
Private Function CorrectAnswer(ByVal questionId As Integer) As String
Dim answer As String = ""
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Answer FROM AnswerTable WHERE QuestionId = @QuestionId", con)
cmd.Parameters.AddWithValue("@QuestionId", questionId)
con.Open()
answer = cmd.ExecuteScalar().ToString()
con.Close()
End Using
End Using
Return answer
End Function
Screenshot