Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:Button ID="btn_Gen" runat="server" Text="Generate" class="btn btn-danger" OnClick="OnSave" />
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class="text-primary">
<tr>
<th>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="Check_UnCheckAll" />
</th>
<th>Id</th>
<th>Name</th>
<th>Fee</th>
<th>Date</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="RepeaterDB" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="checkboxId" runat="server" AutoPostBack="true" OnCheckedChanged="OnRowCheckedUnchecked" /></td>
<td>
<asp:Label ID="memberid" runat="server" Text='<%# Eval("memberid") %>' /></td>
<td>
<asp:Label ID="name" runat="server" Text='<%# Eval("name") %>' /></td>
<td>
<asp:Label ID="Monthly_Fee" runat="server" Text='<%# Eval("Monthly_Fee") %>' /></td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# DateTime.Now.ToString("MM/dd/yyyy") %>' /></td>
<td><a href="AdminUpdateMember.aspx?id=<%#Eval("memberid") %>" class="btn btn-primary">Update</a></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
</div>
</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#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindRepeater();
}
}
private void BindRepeater()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Fees";
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);
RepeaterDB.DataSource = dt;
RepeaterDB.DataBind();
}
}
}
}
protected void OnSave(object sender, EventArgs e)
{
foreach (RepeaterItem item in RepeaterDB.Items)
{
Label id = item.FindControl("memberid") as Label;
Label fee = item.FindControl("Monthly_Fee") as Label;
if ((item.FindControl("checkboxId") as CheckBox).Checked)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("Sp_Insert_Fee", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@memberid", id.Text);
cmd.Parameters.AddWithValue("@Fee", fee.Text);
cmd.Parameters.AddWithValue("@Gen_Date", DateTime.Now.ToString());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
protected void Check_UnCheckAll(object sender, EventArgs e)
{
foreach (RepeaterItem item in RepeaterDB.Items)
{
(item.FindControl("checkboxId") as CheckBox).Checked = chkAll.Checked;
}
}
protected void OnRowCheckedUnchecked(object sender, EventArgs e)
{
bool isAllChecked = true;
foreach (RepeaterItem item in RepeaterDB.Items)
{
if (!(item.FindControl("checkboxId") as CheckBox).Checked)
{
isAllChecked = false;
break;
}
}
chkAll.Checked = isAllChecked;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Fees"
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)
RepeaterDB.DataSource = dt
RepeaterDB.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In RepeaterDB.Items
Dim id As Label = TryCast(item.FindControl("memberid"), Label)
Dim fee As Label = TryCast(item.FindControl("Monthly_Fee"), Label)
If (TryCast(item.FindControl("checkboxId"), CheckBox)).Checked Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("Sp_Insert_Fee", con)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@memberid", id.Text)
cmd.Parameters.AddWithValue("@Fee", fee.Text)
cmd.Parameters.AddWithValue("@Gen_Date", DateTime.Now.ToString())
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
Next
End Sub
Protected Sub Check_UnCheckAll(ByVal sender As Object, ByVal e As EventArgs)
For Each item As RepeaterItem In RepeaterDB.Items
TryCast(item.FindControl("checkboxId"), CheckBox).Checked = chkAll.Checked
Next
End Sub
Protected Sub OnRowCheckedUnchecked(ByVal sender As Object, ByVal e As EventArgs)
Dim isAllChecked As Boolean = True
For Each item As RepeaterItem In RepeaterDB.Items
If Not (TryCast(item.FindControl("checkboxId"), CheckBox)).Checked Then
isAllChecked = False
Exit For
End If
Next
chkAll.Checked = isAllChecked
End Sub