Hi Ramco1917,
Please refer below sample.
SQL
CREATE TABLE [QuestionToEmpoyees](
[QuestionId] INT IDENTITY (1,1),
[Question] VARCHAR(300),
[Option1] VARCHAR(100),
[Option2] VARCHAR(100),
[Option3] VARCHAR(100),
[Option4] VARCHAR(100)
)
INSERT INTO [QuestionToEmpoyees] VALUES('Capital City of Maharshtra', 'Mumbai', 'Delhi', 'Kolkata', 'Haryana')
INSERT INTO [QuestionToEmpoyees] VALUES('The power to decide an election petition is vested in the', 'Parliament', 'High Court', 'Supreme Court', 'Others')
CREATE TABLE [EmpoyeesAnswers](
[EmployeeId] INT,
[Employee Name] VARCHAR(100)
)
INSERT INTO [EmpoyeesAnswers] VALUES(1, 'Employee-A')
INSERT INTO [EmpoyeesAnswers] VALUES(2, 'Employee-B')
INSERT INTO [EmpoyeesAnswers] VALUES(3, 'Employee-C')
INSERT INTO [EmpoyeesAnswers] VALUES(4, 'Employee-D')
HTML
<asp:Repeater ID="rptQuestions" runat="server" OnItemDataBound="itemDataBoundQuestion">
<HeaderTemplate>
<table>
<tr></tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>Question
<asp:Label ID="lblQuestionId" runat="server" Text='<%# Eval("QuestionId") %>' />:</td>
<td colspan="4"><asp:Label ID="lblQuestion" runat="server" Text='<%# Eval("Question") %>' /></td>
</tr>
<tr>
<asp:HiddenField ID="hfQuestionID" runat="server" Value='<%#Eval("QuestionId")%>' />
<asp:Repeater ID="rptAnswers" runat="server">
<ItemTemplate>
<tr>
<%-- <td>
<asp:Label ID="lblEmployeeId" runat="server" Text='<%# Eval("EmployeeId") %>' />
</td>--%>
<td><asp:Label ID="lblEmployeeName" runat="server" Text='<%# Eval("[Employee Name]") %>' />:
</td>
<td><asp:Label ID="lblOpt1" runat="server" Text='<%# Eval("Option1") %>' /></td>
<td><asp:Label ID="lblOpt2" runat="server" Text='<%# Eval("Option2") %>' /></td>
<td><asp:Label ID="lblOpt3" runat="server" Text='<%# Eval("Option3") %>' /></td>
<td><asp:Label ID="lblOpt4" runat="server" Text='<%# Eval("Option4") %>' /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
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#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
rptQuestions.DataSource = GetData("SELECT QuestionId, Question, Option1, Option2, Option3, Option4 FROM QuestionToEmpoyees");
rptQuestions.DataBind();
}
}
private static DataTable GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void itemDataBoundQuestion(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string questionId = (e.Item.FindControl("hfQuestionID") as HiddenField).Value;
Repeater rptAnswer = e.Item.FindControl("rptAnswers") as Repeater;
rptAnswer.DataSource = GetData(string.Format("SELECT EmployeeId,[Employee Name],QuestionId,Question,Option1,Option2,Option3,Option4 FROM QuestionToEmpoyees,EmpoyeesAnswers WHERE QuestionId='{0}'", questionId));
rptAnswer.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
rptQuestions.DataSource = GetData("SELECT QuestionId, Question, Option1, Option2, Option3, Option4 FROM QuestionToEmpoyees")
rptQuestions.DataBind()
End If
End Sub
Private Shared Function GetData(ByVal query As String) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = query
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using ds As DataSet = New DataSet()
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Using
End Function
Protected Sub itemDataBoundQuestion(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("hfQuestionID"), HiddenField)).Value
Dim rptAnswer As Repeater = TryCast(e.Item.FindControl("rptAnswers"), Repeater)
rptAnswer.DataSource = GetData(String.Format("SELECT EmployeeId,[Employee Name],QuestionId,Question,Option1,Option2,Option3,Option4 FROM QuestionToEmpoyees,EmpoyeesAnswers WHERE QuestionId='{0}'", questionId))
rptAnswer.DataBind()
End If
End Sub
Screenshot