Refer below code.
HTML
<asp:Repeater ID="rptQNA" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table class="table">
<tr>
<th scope="col" style="width: auto">Id </th>
<th scope="col" style="width: auto">Question </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblQuestionId" runat="server" Text='<%# Eval("QUESTIONID") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblDescription" runat="server" Text='<%# Eval("QUESTIONDESCRIPTION") %>' Font-Bold="true"></asp:Label>
<br />
<asp:RadioButtonList ID="rblOptions" runat="server" BorderStyle="None">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button runat="server" Text="Submit" OnClick="ValidateAnswer" />
<br />
<asp:Label ID="Label1" runat="server" Text="Incorrect Answer"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="Incorrect Answer"></asp:Label>
<br />
<asp:Label ID="Label3" runat="server" Text="Incorrect Answer"></asp:Label>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindQuestionRepeater();
}
}
private void BindQuestionRepeater()
{
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT QUESTIONID, QUESTIONDESCRIPTION FROM QuestionTable";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter oda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
oda.Fill(dt);
rptQNA.DataSource = dt;
rptQNA.DataBind();
}
}
}
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string questionId = (e.Item.FindControl("lblQuestionId") as Label).Text;
RadioButtonList rblOptions = e.Item.FindControl("rblOptions") as RadioButtonList;
BindradioButtonList(questionId, rblOptions);
}
}
private void BindradioButtonList(string questionId, RadioButtonList rblOptions)
{
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT QUESTIONID, OPTIONS FROM OPTIONTABLE WHERE QUESTIONID = @QuestionId";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("QuestionId", questionId);
cmd.Connection = con;
con.Open();
rblOptions.DataSource = cmd.ExecuteReader();
rblOptions.DataTextField = "OPTIONS";
rblOptions.DataValueField = "OPTIONS";
rblOptions.DataBind();
con.Close();
}
}
}
protected void ValidateAnswer(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptQNA.Items)
{
string id = (item.FindControl("lblQuestionId") as Label).Text;
string answer = (item.FindControl("rblOptions") as RadioButtonList).SelectedItem.Text;
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT QuestionId FROM AnswerTable WHERE QuestionId = @Id AND Answer = @Answer";
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.Parameters.AddWithValue("@Id", id);
cmd.Parameters.AddWithValue("@Answer", answer);
object o = cmd.ExecuteScalar();
con.Close();
switch (id)
{
case "1":
Label1.Text = o != null ? "Answer is correct." : "Incorrect Answer";
break;
case "2":
Label2.Text = o != null ? "Answer is correct." : "Incorrect Answer";
break;
case "3":
Label3.Text = o != null ? "Answer is correct." : "Incorrect Answer";
break;
default:
break;
}
}
}
}
}
VB.Net
Private conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindQuestionRepeater()
End If
End Sub
Private Sub BindQuestionRepeater()
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT QUESTIONID, QUESTIONDESCRIPTION FROM QuestionTable"
Using cmd As SqlCommand = New SqlCommand(query, con)
Using oda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
oda.Fill(dt)
rptQNA.DataSource = dt
rptQNA.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim questionId As String = (TryCast(e.Item.FindControl("lblQuestionId"), Label)).Text
Dim rblOptions As RadioButtonList = TryCast(e.Item.FindControl("rblOptions"), RadioButtonList)
BindradioButtonList(questionId, rblOptions)
End If
End Sub
Private Sub BindradioButtonList(ByVal questionId As String, ByVal rblOptions As RadioButtonList)
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT QUESTIONID, OPTIONS FROM OPTIONTABLE WHERE QUESTIONID = @QuestionId"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("QuestionId", questionId)
cmd.Connection = con
con.Open()
rblOptions.DataSource = cmd.ExecuteReader()
rblOptions.DataTextField = "OPTIONS"
rblOptions.DataValueField = "OPTIONS"
rblOptions.DataBind()
con.Close()
End Using
End Using
End Sub
Protected Sub ValidateAnswer(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In rptQNA.Items
Dim id As String = (TryCast(item.FindControl("lblQuestionId"), Label)).Text
Dim answer As String = (TryCast(item.FindControl("rblOptions"), RadioButtonList)).SelectedItem.Text
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT QuestionId FROM AnswerTable WHERE QuestionId = @Id AND Answer = @Answer"
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
cmd.Parameters.AddWithValue("@Id", id)
cmd.Parameters.AddWithValue("@Answer", answer)
Dim o As Object = cmd.ExecuteScalar()
con.Close()
Select Case id
Case "1"
Label1.Text = If(o IsNot Nothing, "Answer is correct.", "Incorrect Answer")
Case "2"
Label2.Text = If(o IsNot Nothing, "Answer is correct.", "Incorrect Answer")
Case "3"
Label3.Text = If(o IsNot Nothing, "Answer is correct.", "Incorrect Answer")
Case Else
End Select
End Using
End Using
Next
End Sub
Screenshot