Hi careless,
Check this example. Now please take its reference and correct your code.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="OnItemDataBound">
<HeaderTemplate>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
<th>Action</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblId" runat="server" /></td>
<td><asp:Label ID="lblName" runat="server" /></td>
<td><asp:Label ID="lblCountry" runat="server" /></td>
<td><asp:Button Text="Delete" runat="server" OnClientClick="return confirm('Do you want to delete this Customer?');" OnClick="OnDelete" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
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)
{
this.BindRepeater();
}
}
protected void OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
Label id = (Label)e.Item.FindControl("lblId");
Label name = (Label)e.Item.FindControl("lblName");
Label country = (Label)e.Item.FindControl("lblCountry");
id.Text = drv["CustomerId"].ToString();
name.Text = drv["Name"].ToString();
country.Text = drv["Country"].ToString();
}
}
protected void OnDelete(object sender, EventArgs e)
{
Label lblId = ((sender as Button).NamingContainer as RepeaterItem).FindControl("lblId") as Label;
int id = Convert.ToInt32(lblId.Text);
string query = "DELETE FROM Customers WHERE CustomerId=@CustomerId";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@CustomerId", id);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.BindRepeater();
}
private void BindRepeater()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
rptCustomers.DataSource = dt;
rptCustomers.DataBind();
}
}
}
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
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 drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim id As Label = CType(e.Item.FindControl("lblId"), Label)
Dim name As Label = CType(e.Item.FindControl("lblName"), Label)
Dim country As Label = CType(e.Item.FindControl("lblCountry"), Label)
id.Text = drv("CustomerId").ToString()
name.Text = drv("Name").ToString()
country.Text = drv("Country").ToString()
End If
End Sub
Protected Sub OnDelete(ByVal sender As Object, ByVal e As EventArgs)
Dim lblId As Label = TryCast((TryCast((TryCast(sender, Button)).NamingContainer, RepeaterItem)).FindControl("lblId"), Label)
Dim id As Integer = Convert.ToInt32(lblId.Text)
Dim query As String = "DELETE FROM Customers WHERE CustomerId=@CustomerId"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@CustomerId", id)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindRepeater()
End Sub
Private Sub BindRepeater()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT * FROM Customers"
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
rptCustomers.DataSource = dt
rptCustomers.DataBind()
End Using
End Using
End Sub
Screenshot