In this article I will explain with example how to perform select, insert, edit, update, delete in Repeater control in ASP.Net using C# and VB.Net.
	
		This process is also known as CRUD i.e. Create, Read, Update and Delete in Repeater control with SQL Server database backend in ASP.Net.
	
		 
	
		 
	
		Database
	
		I have made use of the following table Customers with the schema as follows.
	
	
		I have already inserted few records in the table.
	
	
		 
	
		
			Note: You can download the database table SQL by clicking the download link below.
		
	 
	
		 
	
		 
	
		HTML Markup
	
		The HTML Markup consists of an ASP.Net Repeater control with multiple buttons for edit, update, cancel and delete operations.
	
		The Delete button is assigned OnClientClick event for raising JavaScript confirmation box when a record is being deleted by the user.
	
		The CustomerId field (Primary Key) is bound to a hidden Label control. The value of the CustomerId will be used for updating and deleting records.
	
		Below the Repeater control there’s a Form which will allow us to insert data to the SQL Server database table.
	
		
			<asp:Repeater ID="Repeater1" runat="server">
		
			    <HeaderTemplate>
		
			        <table cellspacing="0" rules="all" border="1">
		
			            <tr>
		
			                <th scope="col" style="width: 120px">
		
			                    Name
		
			                </th>
		
			                <th scope="col" style="width: 100px">
		
			                    Country
		
			                </th>
		
			                <th scope="col" style="width: 80px">
		
			                </th>
		
			            </tr>
		
			    </HeaderTemplate>
		
			    <ItemTemplate>
		
			        <tr>
		
			            <td>
		
			                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("CustomerId") %>' Visible = "false" />
		
			                <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Name") %>' />
		
			                <asp:TextBox ID="txtContactName" runat="server" Width="120" Text='<%# Eval("Name") %>'
		
			                    Visible="false" />
		
			            </td>
		
			            <td>
		
			                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>' />
		
			                <asp:TextBox ID="txtCountry" runat="server" Width="120" Text='<%# Eval("Country") %>'
		
			                    Visible="false" />
		
			            </td>
		
			            <td>
		
			                <asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" OnClick="OnEdit" />
		
			                <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" Visible="false" OnClick="OnUpdate" />
		
			                <asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" Visible="false" OnClick="OnCancel" />
		
			                <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" OnClick="OnDelete" OnClientClick="return confirm('Do you want to delete this row?');" />
		
			            </td>
		
			        </tr>
		
			    </ItemTemplate>
		
			    <FooterTemplate>
		
			        </table>
		
			    </FooterTemplate>
		
			</asp:Repeater>
		
			<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
		
			    <tr>
		
			        <td style="width: 150px">
		
			            Name:<br />
		
			            <asp:TextBox ID="txtName" runat="server" Width="140" />
		
			        </td>
		
			        <td style="width: 150px">
		
			            Country:<br />
		
			            <asp:TextBox ID="txtCountry" runat="server" Width="140" />
		
			        </td>
		
			        <td style="width: 100px">
		
			            <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />
		
			        </td>
		
			    </tr>
		
			</table>
	 
	
		 
	
		 
	
		Stored Procedure for Select, Insert, Update and Delete
	
		The following stored procedure will be used to perform Select, Insert, Update and Delete operations on the SQL Server database table.
	
		
			CREATE PROCEDURE [dbo].[Customers_CRUD]
		
			      @Action VARCHAR(10)
		
			      ,@CustomerId INT = NULL
		
			      ,@Name VARCHAR(100) = NULL
		
			      ,@Country VARCHAR(100) = NULL
		
			AS
		
			BEGIN
		
			      SET NOCOUNT ON;
		
			 
		
			      --SELECT
		
			    IF @Action = 'SELECT'
		
			      BEGIN
		
			            SELECT CustomerId, Name, Country 
		
			            FROM Customers
		
			      END
		
			 
		
			      --INSERT
		
			    IF @Action = 'INSERT'
		
			      BEGIN
		
			            INSERT INTO Customers(Name, Country) 
		
			            VALUES (@Name, @Country)
		
			      END
		
			 
		
			      --UPDATE
		
			    IF @Action = 'UPDATE'
		
			      BEGIN
		
			            UPDATE Customers 
		
			            SET Name = @Name, Country = @Country 
		
			            WHERE CustomerId = @CustomerId
		
			      END
		
			 
		
			      --DELETE
		
			    IF @Action = 'DELETE'
		
			      BEGIN
		
			            DELETE FROM Customers 
		
			            WHERE CustomerId = @CustomerId
		
			      END
		
			END
	 
	
		 
	
		 
	
		Namespaces
	
		You will need to import the following namespaces.
	
		C#
	
		
			using System.Data;
		
			using System.Configuration;
		
			using System.Data.SqlClient;
	 
	
		 
	
		VB.Net
	
		
			Imports System.Data
		
			Imports System.Configuration
		
			Imports System.Data.SqlClient
	 
	
		 
	
		 
	
		Binding the Repeater with records from SQL Database Table
	
		The Repeater control is populated from the database inside the Page Load event of the page. Here the stored procedure is being called with the Action parameter value as “SELECT”.
	
		C#
	
		
			protected void Page_Load(object sender, EventArgs e)
		
			{
		
			    if (!this.IsPostBack)
		
			    {
		
			        this.BindRepeater();
		
			    }
		
			}
		
			 
		
			private void BindRepeater()
		
			{
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
		
			        {
		
			            cmd.Parameters.AddWithValue("@Action", "SELECT");
		
			            using (SqlDataAdapter sda = new SqlDataAdapter())
		
			            {
		
			                cmd.CommandType = CommandType.StoredProcedure;
		
			                cmd.Connection = con;
		
			                sda.SelectCommand = cmd;
		
			                using (DataTable dt = new DataTable())
		
			                {
		
			                    sda.Fill(dt);
		
			                    Repeater1.DataSource = dt;
		
			                    Repeater1.DataBind();
		
			                }
		
			            }
		
			        }
		
			    }
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
		
			    If Not Me.IsPostBack Then
		
			        Me.BindRepeater()
		
			    End If
		
			End Sub
		
			 
		
			Private Sub BindRepeater()
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("Customers_CRUD")
		
			            cmd.Parameters.AddWithValue("@Action", "SELECT")
		
			            Using sda As New SqlDataAdapter()
		
			                cmd.CommandType = CommandType.StoredProcedure
		
			                cmd.Connection = con
		
			                sda.SelectCommand = cmd
		
			                Using dt As New DataTable()
		
			                    sda.Fill(dt)
		
			                    Repeater1.DataSource = dt
		
			                    Repeater1.DataBind()
		
			                End Using
		
			            End Using
		
			        End Using
		
			    End Using
		
			End Sub
	 
	
		 
	
	
		 
	
		 
	
		Inserting records to Repeater
	
		The following event handler is executed when the Add Button is clicked. The name and the country values are fetched from their respective TextBoxes and then passed to the Stored Procedure with Action parameter value as “INSERT”. 
	
		Finally the Repeater control is again populated with data by making call to the BindRepeater method.
	
		C#
	
		
			protected void Insert(object sender, EventArgs e)
		
			{
		
			    string name = txtName.Text;
		
			    string country = txtCountry.Text;
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
		
			        {
		
			            cmd.CommandType = CommandType.StoredProcedure;
		
			            cmd.Parameters.AddWithValue("@Action", "INSERT");
		
			            cmd.Parameters.AddWithValue("@Name", name);
		
			            cmd.Parameters.AddWithValue("@Country", country);
		
			            cmd.Connection = con;
		
			            con.Open();
		
			            cmd.ExecuteNonQuery();
		
			            con.Close();
		
			        }
		
			    }
		
			    this.BindRepeater();
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub Insert(sender As Object, e As EventArgs)
		
			    Dim name As String = txtName.Text
		
			    Dim country As String = txtCountry.Text
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("Customers_CRUD")
		
			            cmd.CommandType = CommandType.StoredProcedure
		
			            cmd.Parameters.AddWithValue("@Action", "INSERT")
		
			            cmd.Parameters.AddWithValue("@Name", name)
		
			            cmd.Parameters.AddWithValue("@Country", country)
		
			            cmd.Connection = con
		
			            con.Open()
		
			            cmd.ExecuteNonQuery()
		
			            con.Close()
		
			        End Using
		
			    End Using
		
			    Me.BindRepeater()
		
			End Sub
	 
	
		 
	
		 
	
		Editing and Updating Repeater records
	
		Edit
	
		When the Edit Button is clicked, the following event handler is triggered. Here the reference of the Repeater item is determined and is passed to the ToggleElements method with the isEdit parameter passed as True.
	
		When the isEdit parameter is passed as True, the ToggleElements method, hides the Label controls, Edit and Delete buttons and displays the TextBoxes, Update and Cancel buttons for the reference Repeater Item.
	
		C#
	
		
			protected void OnEdit(object sender, EventArgs e)
		
			{
		
			    //Find the reference of the Repeater Item.
		
			    RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;
		
			    this.ToggleElements(item, true);
		
			}
		
			 
		
			private void ToggleElements(RepeaterItem item, bool isEdit)
		
			{
		
			    //Toggle Buttons.
		
			    item.FindControl("lnkEdit").Visible = !isEdit;
		
			    item.FindControl("lnkUpdate").Visible = isEdit;
		
			    item.FindControl("lnkCancel").Visible = isEdit;
		
			    item.FindControl("lnkDelete").Visible = !isEdit;
		
			 
		
			    //Toggle Labels.
		
			    item.FindControl("lblContactName").Visible = !isEdit;
		
			    item.FindControl("lblCountry").Visible = !isEdit;
		
			 
		
			    //Toggle TextBoxes.
		
			    item.FindControl("txtContactName").Visible = isEdit;
		
			    item.FindControl("txtCountry").Visible = isEdit;
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub OnEdit(sender As Object, e As EventArgs)
		
			    'Find the reference of the Repeater Item.
		
			    Dim item As RepeaterItem = TryCast(TryCast(sender, LinkButton).Parent, RepeaterItem)
		
			    Me.ToggleElements(item, True)
		
			End Sub
		
			 
		
			Private Sub ToggleElements(item As RepeaterItem, isEdit As Boolean)
		
			    'Toggle Buttons.
		
			    item.FindControl("lnkEdit").Visible = Not isEdit
		
			    item.FindControl("lnkUpdate").Visible = isEdit
		
			    item.FindControl("lnkCancel").Visible = isEdit
		
			    item.FindControl("lnkDelete").Visible = Not isEdit
		
			 
		
			    'Toggle Labels.
		
			    item.FindControl("lblContactName").Visible = Not isEdit
		
			    item.FindControl("lblCountry").Visible = Not isEdit
		
			 
		
			    'Toggle TextBoxes.
		
			    item.FindControl("txtContactName").Visible = isEdit
		
			    item.FindControl("txtCountry").Visible = isEdit
		
			End Sub
	 
	
		 
	
		Update
	
		When the Update Button is clicked, the following event handler is triggered. 
	
		CustomerId which is the primary key is fetched from the hidden Label control while the Name and Country fields are fetched from their respective TextBoxes and are passed to the Stored Procedure with Action parameter value as “UPDATE”.
	
		C#
	
		
			protected void OnUpdate(object sender, EventArgs e)
		
			{
		
			    //Find the reference of the Repeater Item.
		
			    RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;
		
			    int customerId = int.Parse((item.FindControl("lblCustomerId") as Label).Text);
		
			    string name = (item.FindControl("txtContactName") as TextBox).Text.Trim();
		
			    string country = (item.FindControl("txtCountry") as TextBox).Text.Trim();
		
			 
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
		
			        {
		
			            cmd.CommandType = CommandType.StoredProcedure;
		
			            cmd.Parameters.AddWithValue("@Action", "UPDATE");
		
			            cmd.Parameters.AddWithValue("@CustomerId", customerId);
		
			            cmd.Parameters.AddWithValue("@Name", name);
		
			            cmd.Parameters.AddWithValue("@Country", country);
		
			            cmd.Connection = con;
		
			            con.Open();
		
			            cmd.ExecuteNonQuery();
		
			            con.Close();
		
			        }
		
			    }
		
			    this.BindRepeater();
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub OnUpdate(sender As Object, e As EventArgs)
		
			    'Find the reference of the Repeater Item.
		
			    Dim item As RepeaterItem = TryCast(TryCast(sender, LinkButton).Parent, RepeaterItem)
		
			    Dim customerId As Integer = Integer.Parse(TryCast(item.FindControl("lblCustomerId"), Label).Text)
		
			    Dim name As String = TryCast(item.FindControl("txtContactName"), TextBox).Text.Trim()
		
			    Dim country As String = TryCast(item.FindControl("txtCountry"), TextBox).Text.Trim()
		
			 
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("Customers_CRUD")
		
			            cmd.CommandType = CommandType.StoredProcedure
		
			            cmd.Parameters.AddWithValue("@Action", "UPDATE")
		
			            cmd.Parameters.AddWithValue("@CustomerId", customerId)
		
			            cmd.Parameters.AddWithValue("@Name", name)
		
			            cmd.Parameters.AddWithValue("@Country", country)
		
			            cmd.Connection = con
		
			            con.Open()
		
			            cmd.ExecuteNonQuery()
		
			            con.Close()
		
			        End Using
		
			    End Using
		
			    Me.BindRepeater()
		
			End Sub
	 
	
		 
	
		Cancel Edit
	
		When the Cancel Button is clicked, the following event handler is triggered. Here the reference of the Repeater item is determined and is passed to the ToggleElements method with the isEdit parameter passed as False.
	
		When the isEdit parameter is passed as False, the ToggleElements method, hides the TextBox controls, Update and Cancel buttons and displays the Labels, Edit and Delete buttons for the reference Repeater Item.
	
		C#
	
		
			protected void OnCancel(object sender, EventArgs e)
		
			{
		
			    //Find the reference of the Repeater Item.
		
			    RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;
		
			    this.ToggleElements(item, false);
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub OnCancel(sender As Object, e As EventArgs)
		
			    'Find the reference of the Repeater Item.
		
			    Dim item As RepeaterItem = TryCast(TryCast(sender, LinkButton).Parent, RepeaterItem)
		
			    Me.ToggleElements(item, False)
		
			End Sub
	 
	
		 
	
	
		 
	
		 
	
		Deleting Repeater records
	
		When the Delete Button is clicked, the following event handler is triggered. 
	
		CustomerId which is the primary key is fetched from the hidden Label control and is passed to the Stored Procedure along with the Action parameter value as “DELETE”.
	
		C#
	
		
			protected void OnDelete(object sender, EventArgs e)
		
			{
		
			    //Find the reference of the Repeater Item.
		
			    RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;
		
			    int customerId = int.Parse((item.FindControl("lblCustomerId") as Label).Text);
		
			 
		
			    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
		
			    using (SqlConnection con = new SqlConnection(constr))
		
			    {
		
			        using (SqlCommand cmd = new SqlCommand("Customers_CRUD"))
		
			        {
		
			            cmd.CommandType = CommandType.StoredProcedure;
		
			            cmd.Parameters.AddWithValue("@Action", "DELETE");
		
			            cmd.Parameters.AddWithValue("@CustomerId", customerId);
		
			            cmd.Connection = con;
		
			            con.Open();
		
			            cmd.ExecuteNonQuery();
		
			            con.Close();
		
			        }
		
			    }
		
			    this.BindRepeater();
		
			}
	 
	
		 
	
		VB.Net
	
		
			Protected Sub OnDelete(sender As Object, e As EventArgs)
		
			    'Find the reference of the Repeater Item.
		
			    Dim item As RepeaterItem = TryCast(TryCast(sender, LinkButton).Parent, RepeaterItem)
		
			    Dim customerId As Integer = Integer.Parse(TryCast(item.FindControl("lblCustomerId"), Label).Text)
		
			 
		
			    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		
			    Using con As New SqlConnection(constr)
		
			        Using cmd As New SqlCommand("Customers_CRUD")
		
			            cmd.CommandType = CommandType.StoredProcedure
		
			            cmd.Parameters.AddWithValue("@Action", "DELETE")
		
			            cmd.Parameters.AddWithValue("@CustomerId", customerId)
		
			            cmd.Connection = con
		
			            con.Open()
		
			            cmd.ExecuteNonQuery()
		
			            con.Close()
		
			        End Using
		
			    End Using
		
			    Me.BindRepeater()
		
			End Sub
	 
	
		 
	
	
		 
	
		 
	
		Downloads