Hi setwell,
Refer below sample code.
Database
CREATE TABLE FeedbackTable
(
FeedbackId INT NOT NULL
,FeedbackDescription VARCHAR(500) NOT NULL
);
INSERT INTO FeedbackTable VALUES(1,'Are you satisfied with our services?');
INSERT INTO FeedbackTable VALUES(2,'Do you wants to continue our services?');
INSERT INTO FeedbackTable VALUES(3,'Is your problem solved?');
INSERT INTO FeedbackTable VALUES(4,'Where you stays?');
GO
CREATE TABLE FeedbackOptionTable
(
FeedbackId INT NOT NULL
,FeedbackOptions VARCHAR(500) NOT NULL
);
INSERT INTO FeedbackOptionTable VALUES(1,'Satisfied');
INSERT INTO FeedbackOptionTable VALUES(1,'Extremely Satisfied');
INSERT INTO FeedbackOptionTable VALUES(1,'Not Satisfied');
INSERT INTO FeedbackOptionTable VALUES(2,'Yes');
INSERT INTO FeedbackOptionTable VALUES(2,'No');
INSERT INTO FeedbackOptionTable VALUES(2,'Tentative');
INSERT INTO FeedbackOptionTable VALUES(3,'Resolved');
INSERT INTO FeedbackOptionTable VALUES(3,'Not Resolved');
INSERT INTO FeedbackOptionTable VALUES(3,'Under Process');
INSERT INTO FeedbackOptionTable VALUES(4,'India');
INSERT INTO FeedbackOptionTable VALUES(4,'Outside India');
INSERT INTO FeedbackOptionTable VALUES(4,'NRI');
GO
CREATE TABLE FeedbackResultTable
(
EmployeeId VARCHAR(20)
,Employee_Name VARCHAR(50)
,Department VARCHAR(50)
,FeedbakId INT NOT NULL
,FeedbackDescription VARCHAR(500) NOT NULL
,Suggestions VARCHAR(500)
,ResultQuestion1 VARCHAR(200)
,ResultQuestion2 VARCHAR(200)
,ResultQuestion3 VARCHAR(200)
,ResultQuestion4 VARCHAR(200)
,ResultQuestion5 VARCHAR(200)
,ResultQuestion6 VARCHAR(200)
,ResultQuestion7 VARCHAR(200)
,ResultQuestion8 VARCHAR(200)
,ResultQuestion9 VARCHAR(200)
,ResultQuestion10 VARCHAR(200)
);
HTML
<div class="container">
<asp:Repeater ID="rptFeedback" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table class="table table-hover">
<tr>
<th scope="col" style="width: auto">Feedback Id </th>
<th scope="col" style="width: auto">Feedback Question </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblFeedbackId" runat="server" Text='<%# Eval("FeedbackId") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblFeedbackDescription" runat="server" Text='<%# Eval("FeedbackDescription") %>' Font-Bold="true"></asp:Label>
<br />
<asp:RadioButtonList CssClass="table" ID="rblFeedbackOptions" runat="server" RepeatDirection="Horizontal">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>Suggestions <span style="font-size: x-large; color: red">*</span></td>
<td>
<asp:TextBox runat="server" TextMode="MultiLine" Width="100%" required ID="txtSuggestion" placeholder="Please post your Suggestions..." /></td>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<asp:Button runat="server" CssClass="btn-success center-block btn-lg" OnClick="OnSubmitFeedback" Text="Submit Feedback" ForeColor="Black" Font-Bold="true" />
</div>
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#
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindFeedbackRepeater();
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string questionId = (e.Item.FindControl("lblFeedbackId") as Label).Text;
RadioButtonList rblOptions = e.Item.FindControl("rblFeedbackOptions") as RadioButtonList;
BindradioButtonList(questionId, rblOptions);
}
}
protected void OnSubmitFeedback(object sender, EventArgs e)
{
foreach (RepeaterItem rptItem in rptFeedback.Items)
{
string feedbackId = ((Label)rptItem.FindControl("lblFeedbackId")).Text;
string feedbackDescription = ((Label)rptItem.FindControl("lblFeedbackDescription")).Text;
string suggestion = (rptFeedback.Controls[rptFeedback.Controls.Count - 1].FindControl("txtSuggestion") as TextBox).Text;
RadioButtonList rblOptions = rptItem.FindControl("rblFeedbackOptions") as RadioButtonList;
string query = "INSERT INTO FeedbackResultTable (EmployeeId, Employee_Name, Department, FeedbakId, FeedbackDescription, Suggestions,";
query += " ResultQuestion1, ResultQuestion2, ResultQuestion3, ResultQuestion4, ResultQuestion5,";
query += " ResultQuestion6, ResultQuestion7, ResultQuestion8, ResultQuestion9, ResultQuestion10)";
query += " VALUES ('{EmployeeId}', '{Employee_Name}', '{Department}', '{FeedbakId}', '{FeedbackDescription}', '{Suggestions}',";
query += " '{ResultQuestion1}', '{ResultQuestion2}', '{ResultQuestion3}', '{ResultQuestion4}', '{ResultQuestion5}',";
query += " '{ResultQuestion6}', '{ResultQuestion7}', '{ResultQuestion8}', '{ResultQuestion9}', '{ResultQuestion10}')";
query = query.Replace("{EmployeeId}", "1");
query = query.Replace("{Employee_Name}", "Dharmenda");
query = query.Replace("{Department}", "IT");
query = query.Replace("{FeedbakId}", feedbackId);
query = query.Replace("{FeedbackDescription}", feedbackDescription);
query = query.Replace("{Suggestions}", suggestion);
for (int i = 1; i <= 10; i++)
{
string columnName = "{ResultQuestion" + i + "}";
if (i <= rblOptions.Items.Count)
{
query = query.Replace(columnName, rblOptions.Items[i - 1].Selected ? rblOptions.Items[i - 1].Value : string.Empty);
}
else
{
query = query.Replace(columnName, string.Empty);
}
}
// Insert code.
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
private void BindFeedbackRepeater()
{
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT FeedbackId, FeedbackDescription FROM FeedbackTable";
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter oda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
oda.Fill(dt);
rptFeedback.DataSource = dt;
rptFeedback.DataBind();
}
}
}
}
}
private void BindradioButtonList(string feedbackId, RadioButtonList rblOptions)
{
using (SqlConnection con = new SqlConnection(conString))
{
string query = "SELECT FeedbackId, FeedbackOptions FROM FeedbackOptionTable WHERE FeedbackId = @Id";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Id", feedbackId);
cmd.Connection = con;
con.Open();
rblOptions.DataSource = cmd.ExecuteReader();
rblOptions.DataTextField = "FeedbackOptions";
rblOptions.DataValueField = "FeedbackOptions";
rblOptions.DataBind();
con.Close();
}
}
}
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.BindFeedbackRepeater()
End If
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("lblFeedbackId"), Label)).Text
Dim rblOptions As RadioButtonList = TryCast(e.Item.FindControl("rblFeedbackOptions"), RadioButtonList)
BindradioButtonList(questionId, rblOptions)
End If
End Sub
Protected Sub OnSubmitFeedback(ByVal sender As Object, ByVal e As EventArgs)
For Each rptItem As RepeaterItem In rptFeedback.Items
Dim feedbackId As String = (CType(rptItem.FindControl("lblFeedbackId"), Label)).Text
Dim feedbackDescription As String = (CType(rptItem.FindControl("lblFeedbackDescription"), Label)).Text
Dim suggestion As String = (TryCast(rptFeedback.Controls(rptFeedback.Controls.Count - 1).FindControl("txtSuggestion"), TextBox)).Text
Dim rblOptions As RadioButtonList = TryCast(rptItem.FindControl("rblFeedbackOptions"), RadioButtonList)
Dim query As String = "INSERT INTO FeedbackResultTable (EmployeeId, Employee_Name, Department, FeedbakId, FeedbackDescription, Suggestions,"
query += " ResultQuestion1, ResultQuestion2, ResultQuestion3, ResultQuestion4, ResultQuestion5,"
query += " ResultQuestion6, ResultQuestion7, ResultQuestion8, ResultQuestion9, ResultQuestion10)"
query += " VALUES ('{EmployeeId}', '{Employee_Name}', '{Department}', '{FeedbakId}', '{FeedbackDescription}', '{Suggestions}',"
query += " '{ResultQuestion1}', '{ResultQuestion2}', '{ResultQuestion3}', '{ResultQuestion4}', '{ResultQuestion5}',"
query += " '{ResultQuestion6}', '{ResultQuestion7}', '{ResultQuestion8}', '{ResultQuestion9}', '{ResultQuestion10}')"
query = query.Replace("{EmployeeId}", "1")
query = query.Replace("{Employee_Name}", "Dharmenda")
query = query.Replace("{Department}", "IT")
query = query.Replace("{FeedbakId}", feedbackId)
query = query.Replace("{FeedbackDescription}", feedbackDescription)
query = query.Replace("{Suggestions}", suggestion)
For i As Integer = 1 To 10
Dim columnName As String = "{ResultQuestion" & i & "}"
If i <= rblOptions.Items.Count Then
query = query.Replace(columnName, If(rblOptions.Items(i - 1).Selected, rblOptions.Items(i - 1).Value, String.Empty))
Else
query = query.Replace(columnName, String.Empty)
End If
Next
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Private Sub BindFeedbackRepeater()
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT FeedbackId, FeedbackDescription FROM FeedbackTable"
Using cmd As SqlCommand = New SqlCommand(query, con)
Using oda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
oda.Fill(dt)
rptFeedback.DataSource = dt
rptFeedback.DataBind()
End Using
End Using
End Using
End Using
End Sub
Private Sub BindradioButtonList(ByVal feedbackId As String, ByVal rblOptions As RadioButtonList)
Using con As SqlConnection = New SqlConnection(conString)
Dim query As String = "SELECT FeedbackId, FeedbackOptions FROM FeedbackOptionTable WHERE FeedbackId = @Id"
Using cmd As SqlCommand = New SqlCommand(query, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Id", feedbackId)
cmd.Connection = con
con.Open()
rblOptions.DataSource = cmd.ExecuteReader()
rblOptions.DataTextField = "FeedbackOptions"
rblOptions.DataValueField = "FeedbackOptions"
rblOptions.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot