Hi @ Sangi,
I have made a small snippet for you.
Please try it.
HTML
<div>
<asp:ListView ID="lvUsers" runat="server">
<ItemTemplate>
<asp:CheckBox ID="chkCustomer" runat="server" />
<asp:Label ID="lblCustomerId" runat="server" Text='<%#Eval("CustomerId") %>'></asp:Label>
<asp:Label ID="lblCustomerName" runat="server" Text='<%#Eval("CustomerName") %>'></asp:Label>
<br />
</ItemTemplate>
</asp:ListView>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="Delete" />
</div>
C# Code:-
SqlConnection con;
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
cmd = new SqlCommand("SELECT CustomerId, CustomerName FROM Customers", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
this.lvUsers.DataSource = dt;
this.lvUsers.DataBind();
}
}
protected void Delete(object sender, EventArgs e)
{
foreach (ListViewDataItem item in this.lvUsers.Items)
{
if (item.ItemType == ListViewItemType.DataItem)
{
CheckBox chkUser = item.FindControl("chkCustomer") as CheckBox;
if (chkUser.Checked)
{
int customerId = int.Parse((item.FindControl("lblCustomerId") as Label).Text.Trim());
con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId=@Id", con);
cmd.Parameters.AddWithValue("@Id", customerId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Response.Redirect(Request.Url.AbsolutePath);
}
Namespaces:-
using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
If any confusion,Please revert on the same.