Hi irshad1231,
Check this example. Now please take its reference and correct your code.
Database
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
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
INSERT INTO [dbo].[QuestionTable] VALUES('Who is the Prime minister of India','LK Advani','Manmohan singh','Sachin Tendulker','N. Modi')
INSERT INTO [dbo].[QuestionTable] VALUES('Capital City of Maharashtra','Delhi','Mumbai','Kolkata','Banglore')
INSERT INTO [dbo].[QuestionTable] VALUES('Maximum run in ODI by a batsman','Ricky Pointing','Sachin Tendulker','Azharuddin','Don Bradman')
HTML
<asp:DataList ID="DataList1" runat="server" Width="100%"
OnItemDataBound="DataList1_ItemDataBound">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#string.Format("{0} {1}",Eval("QuestionId"),Eval("QuestionDescription")) %>'></asp:Label>
<asp:RadioButtonList ID="RadioButtonList1" runat="server"></asp:RadioButtonList>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:DataList>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList1.DataSource = PopulateQuestions();
DataList1.DataBind();
}
}
private DataTable PopulateQuestions()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT DISTINCT QuestionId,QuestionDescription FROM QuestionTable";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
private DataTable PopulateOptions(int questionId)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = @"SELECT [OptionOne] Options FROM QuestionTable WHERE QuestionId = @Question
UNION ALL
SELECT[OptionTwo] FROM QuestionTable WHERE QuestionId = @Question
UNION ALL
SELECT[OptionThree] FROM QuestionTable WHERE QuestionId = @Question
UNION ALL
SELECT[OptionFour] FROM QuestionTable WHERE QuestionId = @Question";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Question", questionId);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButtonList rdblist = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
Label lblqid = (Label)e.Item.FindControl("Label1");
string qid = lblqid.Text;
DataTable dt = PopulateOptions(int.Parse(lblqid.Text.Split(' ')[0]));
rdblist.DataSource = dt;
rdblist.DataTextField = "Options";
rdblist.DataValueField = "Options";
rdblist.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
DataList1.DataSource = PopulateQuestions()
DataList1.DataBind()
End If
End Sub
Private Function PopulateQuestions() As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT DISTINCT QuestionId,QuestionDescription FROM QuestionTable"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Private Function PopulateOptions(ByVal questionId As Integer) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT [OptionOne] Options FROM QuestionTable WHERE QuestionId = @Question" _
+ " UNION ALL" _
+ " SELECT[OptionTwo] FROM QuestionTable WHERE QuestionId = @Question" _
+ " UNION ALL" _
+ " SELECT[OptionThree] FROM QuestionTable WHERE QuestionId = @Question" _
+ " UNION ALL" _
+ " SELECT[OptionFour] FROM QuestionTable WHERE QuestionId = @Question"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Question", questionId)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rdblist As RadioButtonList = CType(e.Item.FindControl("RadioButtonList1"), RadioButtonList)
Dim lblqid As Label = CType(e.Item.FindControl("Label1"), Label)
Dim qid As String = lblqid.Text
Dim dt As DataTable = PopulateOptions(Integer.Parse(lblqid.Text.Split(" "c)(0)))
rdblist.DataSource = dt
rdblist.DataTextField = "Options"
rdblist.DataValueField = "Options"
rdblist.DataBind()
End If
End Sub
Screenshot