Here I have created sample that will help you out.
HTML
<div>
<div id="dvQuestion" runat="server">
<h1>
<asp:Label ID="lblQuestion" runat="server" />
</h1>
<br />
<asp:RadioButtonList ID="rbtnOptions" runat="server">
</asp:RadioButtonList>
<br />
<br />
<asp:Button Text="Next" runat="server" OnClick="Next" />
</div>
<div id="dvResult" runat="server" visible="false">
<h1>
<asp:Label ID="lblResult" runat="server" />
</h1>
<br />
<asp:Button Text="Start Again" runat="server" OnClick="Start" />
</div>
</div>
C#
private DataTable DataTableQuestions
{
get { return (DataTable)ViewState["Question"]; }
set { ViewState["Question"] = value; }
}
private int QuestionIndex
{
get { return (int)ViewState["QuestionIndex"]; }
set { ViewState["QuestionIndex"] = value; }
}
private int Score
{
get { return (int)ViewState["Score"]; }
set { ViewState["Score"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTableQuestions = PopulateQuestions();
QuestionIndex = 0;
Score = 0;
GetCurrentQuestion(QuestionIndex, DataTableQuestions);
}
}
protected void Next(object sender, EventArgs e)
{
QuestionIndex++;
GetCurrentQuestion(QuestionIndex, DataTableQuestions, int.Parse(rbtnOptions.SelectedItem.Value));
}
protected void Start(object sender, EventArgs e)
{
Response.Redirect(Request.Url.AbsoluteUri);
}
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
using (SqlCommand _cmd = new SqlCommand("SELECT * FROM QuestionTable", _cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
{
_cn.Open();
da.Fill(dt);
_cn.Close();
}
}
}
return dt;
}
private void GetCurrentQuestion(int index, DataTable dtQuestions, int? selectedOption = null)
{
if (selectedOption != null)
{
string option = string.Empty;
DataRow row = dtQuestions.Rows[index - 1];
if (selectedOption == 0)
{
option = "OptionOne";
}
else if (selectedOption == 1)
{
option = "OptionTwo";
}
else if (selectedOption == 2)
{
option = "OptionThree";
}
else if (selectedOption == 3)
{
option = "OptionFour";
}
if (option == row["CorrectAnswer"].ToString())
{
Score++;
}
}
if (index < dtQuestions.Rows.Count)
{
DataRow row = dtQuestions.Rows[index];
lblQuestion.Text = row["QuestionDescription"].ToString();
ListItem option1 = new ListItem(row["OptionOne"].ToString(), "0");
ListItem option2 = new ListItem(row["OptionTwo"].ToString(), "1");
ListItem option3 = new ListItem(row["OptionThree"].ToString(), "2");
ListItem option4 = new ListItem(row["OptionFour"].ToString(), "3");
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(new ListItem[4] { option1, option2, option3, option4 });
rbtnOptions.DataBind();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, index);
}
}
VB
Private Property DataTableQuestions() As DataTable
Get
Return DirectCast(ViewState("Question"), DataTable)
End Get
Set
ViewState("Question") = value
End Set
End Property
Private Property QuestionIndex() As Integer
Get
Return CInt(ViewState("QuestionIndex"))
End Get
Set
ViewState("QuestionIndex") = value
End Set
End Property
Private Property Score() As Integer
Get
Return CInt(ViewState("Score"))
End Get
Set
ViewState("Score") = value
End Set
End Property
Protected Sub Page_Load(sender As Object, e As EventArgs)
If Not Me.IsPostBack Then
DataTableQuestions = PopulateQuestions()
QuestionIndex = 0
Score = 0
GetCurrentQuestion(QuestionIndex, DataTableQuestions)
End If
End Sub
Protected Sub [Next](sender As Object, e As EventArgs)
QuestionIndex += 1
GetCurrentQuestion(QuestionIndex, DataTableQuestions, Integer.Parse(rbtnOptions.SelectedItem.Value))
End Sub
Protected Sub Start(sender As Object, e As EventArgs)
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Private Function PopulateQuestions() As DataTable
Dim dt As New DataTable()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using _cn As New SqlConnection(constr)
Using _cmd As New SqlCommand("SELECT * FROM QuestionTable", _cn)
Using da As New SqlDataAdapter(_cmd)
_cn.Open()
da.Fill(dt)
_cn.Close()
End Using
End Using
End Using
Return dt
End Function
Private Sub GetCurrentQuestion(index As Integer, dtQuestions As DataTable, Optional selectedOption As System.Nullable(Of Integer) = Nothing)
If selectedOption IsNot Nothing Then
Dim [option] As String = String.Empty
Dim row As DataRow = dtQuestions.Rows(index - 1)
If selectedOption = 0 Then
[option] = "OptionOne"
ElseIf selectedOption = 1 Then
[option] = "OptionTwo"
ElseIf selectedOption = 2 Then
[option] = "OptionThree"
ElseIf selectedOption = 3 Then
[option] = "OptionFour"
End If
If [option] = row("CorrectAnswer").ToString() Then
Score += 1
End If
End If
If index < dtQuestions.Rows.Count Then
Dim row As DataRow = dtQuestions.Rows(index)
lblQuestion.Text = row("QuestionDescription").ToString()
Dim option1 As New ListItem(row("OptionOne").ToString(), "0")
Dim option2 As New ListItem(row("OptionTwo").ToString(), "1")
Dim option3 As New ListItem(row("OptionThree").ToString(), "2")
Dim option4 As New ListItem(row("OptionFour").ToString(), "3")
rbtnOptions.Items.Clear()
rbtnOptions.Items.AddRange(New ListItem(3) {option1, option2, option3, option4})
rbtnOptions.DataBind()
Else
dvQuestion.Visible = False
dvResult.Visible = True
lblResult.Text = String.Format("You Scored {0}/{1}", Score, index)
End If
End Sub
SQL
CREATE TABLE [dbo].[QuestionTable](
[QuestionId] [int] IDENTITY(1,1) NOT NULL,
[QuestionDescription] [varchar](max) NOT NULL,
[OptionOne] [varchar](30) NOT NULL,
[OptionTwo] [varchar](30) NOT NULL,
[OptionThree] [varchar](30) NOT NULL,
[OptionFour] [varchar](30) NOT NULL,
[CorrectAnswer] [varchar](20) NOT NULL,
CONSTRAINT [PK_QuestionTable] PRIMARY KEY CLUSTERED
(
[QuestionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Screenshot
1)
2)
