Hi ramco1917,
Please refer below sample.
HTML
<asp:Repeater ID="rptStudents" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<tr>
<td>StudentId</td>
<td>StudentName</td>
<td>Status</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%#Eval("StudentId") %>' runat="server" /></td>
<td><asp:Label ID="lblName" Text='<%#Eval("StudentName") %>' runat="server" /></td>
<td>
<asp:Label ID="lblStatus" Text='<%#Eval("Status") %>' runat="server" Visible="false" />
<asp:RadioButtonList ID="rbtStatus" runat="server">
</asp:RadioButtonList>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Insert" />
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)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT StudentId, StudentName, Status FROM Students", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptStudents.DataSource = dt;
rptStudents.DataBind();
}
}
}
}
protected void Insert(object sender, EventArgs e)
{
foreach (RepeaterItem item in rptStudents.Items)
{
string status = (item.FindControl("rbtStatus") as RadioButtonList).SelectedItem.Text;
string id = (item.FindControl("lblId") as Label).Text;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Students SET Status = @Status WHERE StudentId = @Id", con))
{
cmd.Parameters.AddWithValue("@Status", status);
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.BindRepeater();
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string status = (e.Item.FindControl("lblStatus") as Label).Text;
RadioButtonList rbtStatus = (e.Item.FindControl("rbtStatus") as RadioButtonList);
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT Status FROM Students", con))
{
con.Open();
rbtStatus.DataSource = cmd.ExecuteReader();
rbtStatus.DataTextField = "Status";
rbtStatus.DataValueField = "Status";
rbtStatus.DataBind();
con.Close();
rbtStatus.Items.FindByText(status).Selected = true;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT StudentId, StudentName, Status FROM Students", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptStudents.DataSource = dt
rptStudents.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In rptStudents.Items
Dim status As String = (TryCast(item.FindControl("rbtStatus"), RadioButtonList)).SelectedItem.Text
Dim id As String = (TryCast(item.FindControl("lblId"), Label)).Text
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("UPDATE Students SET Status = @Status WHERE StudentId = @Id", con)
cmd.Parameters.AddWithValue("@Status", status)
cmd.Parameters.AddWithValue("@Id", id)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
Me.BindRepeater()
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 status As String = (TryCast(e.Item.FindControl("lblStatus"), Label)).Text
Dim rbtStatus As RadioButtonList = (TryCast(e.Item.FindControl("rbtStatus"), RadioButtonList))
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Status FROM Students", con)
con.Open()
rbtStatus.DataSource = cmd.ExecuteReader()
rbtStatus.DataTextField = "Status"
rbtStatus.DataValueField = "Status"
rbtStatus.DataBind()
con.Close()
rbtStatus.Items.FindByText(status).Selected = True
End Using
End Using
End If
End Sub
Screenshot
Database