Friday 7 March 2014

Insert edit update delete in repeater control in asp.net

In this post I will Explain How to Insert, Edit, Update, Delete And display data or CRUD Operation in Repeater control.

The Repeater is control for used in asp.net to display the Entire table data. Repeater control is the best control to display repeated data. It allows to customization of the layout by each repeated list of item and do lots of design customization. The Repeater Control may be bound database file, database table, XML file and other file to control. The Repeater control is very use full Control to  display a repeated data that are bound to the control because we can create Table, Unordered lists, ordered list, anchor tags, Image galleries etc.

For Implementing Insert, Edit, Update and  delete Operation in Repeater first you have to create one table in database.
Create Table name is “dsh_RepeaterDemo"


Next step
1.Open Visual Studio
2.Create a new Project (ASP.NET Web Service) name it “RepeaterDemo” (You   can Also change Project name as you need.).

Next Step
Design aspx page In that page I am use three textbox and two button and one repeater control.
Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RepeaterDemo._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 79px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
            <tr>
                <td class="style2">
                    Name:
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Username:
                </td>
                <td>
                    <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password:
                </td>
                <td>
                    <asp:TextBox ID="txtpass" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                 
                    <asp:Button ID="btnsave" runat="server" Text="Save" OnClick="btnsave_Click" Height="23px"
                        Width="46px" />
                    &nbsp;
                    <asp:Button ID="btncancel" runat="server" Text="Cancel" Height="23px" Width="49px"
                        OnClick="btncancel_Click" />
                    &nbsp;
                </td>
            </tr>
        </table>
        <asp:Repeater ID="repeater" runat="server" OnItemCommand="OnItemCommand_repeater">
            <HeaderTemplate>
                <table border="1">
                    <tr>
                        <td>
                            <asp:Label ID="lblname" runat="server" Text="name"></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblusername" runat="server" Text="username"></asp:Label>
                        </td>
                        <td>
                            <asp:Label ID="lblpass" runat="server" Text="password"></asp:Label>
                        </td>
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:Label ID="labelID" Visible="false" runat="server" Text='<%#Eval("id") %>'></asp:Label>
                        <asp:Label ID="labelname" runat="server" Text='<%#Eval("name") %>'></asp:Label>
                        <asp:TextBox ID="lbltxtname" runat="server" Visible="false" Text='<%#Bind("name") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:Label ID="labelusername" runat="server" Text='<%#Eval("username") %>'></asp:Label>
                        <asp:TextBox ID="lbltxtusername" runat="server" Visible="false" Text='<%#Bind("username") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:Label ID="labelpassword" runat="server" Text='<%#Eval("password") %>'></asp:Label>
                        <asp:TextBox ID="lbltxtpassword" runat="server" Visible="false" Text='<%#Bind("password") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:ImageButton ID="imgedit" runat="server" CommandName="Edit" ImageUrl="img/edit-128.png"
                            Height="25" Width="25" />
                    </td>
                    <td>
                        <asp:ImageButton ID="imgdelete" runat="server" CommandName="Delete" ImageUrl="img/delete-24.png"
                            Height="25" Width="25" />
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="txtftrname" runat="server" Visible="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtftrusername" runat="server" Visible="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="txtftrpassword" runat="server" Visible="true"></asp:TextBox>
                    </td>
                    <td>
                        <asp:LinkButton ID="lnkftrinsert" runat="server" Text="Insert" CommandName="Insert">
                        </asp:LinkButton>
                    </td>
                </tr>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>
Design of aspx page look like below figure.


Here I am used Stored Procedure to Perform Insert, Edit, Update and delete Operation in Repeater control.

StoredProcedure For Insert data to database :


StoredProcedure For Read Data From Database table :

StoredProcedure For Update Data to Database table :


StoredProcedure For Delete Data Of Database Table :


Next Step To code In Default.aspx.cs page
Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace RepeaterDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationTable"].ConnectionString.ToString());
        static int userid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRepeater();
            }
        }
     
        //Save Button
        protected void btnsave_Click(object sender, EventArgs e)
        {
            //Code For Update Data
            if (btnsave.Text == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandText = "usp_updateRepeaterDemo";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", txtname.Text);
                cmd.Parameters.AddWithValue("@username", txtusername.Text);
                cmd.Parameters.AddWithValue("@password", txtpass.Text);
                cmd.Parameters.AddWithValue("@id", userid);
                cmd.ExecuteNonQuery();
                conn.Close();
                clearcontrol();
                BindRepeater();
                btnsave.Text = "Save";
            }
            else
            {
                //Code For Inserting Data to Database Table
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandText = "usp_RepeaterDemo";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", txtname.Text);
                cmd.Parameters.AddWithValue("@username", txtusername.Text);
                cmd.Parameters.AddWithValue("@password", txtpass.Text);
                clearcontrol();
                Response.Write("Record Inserted Successfully!");
                cmd.ExecuteNonQuery();
                conn.Close();
                BindRepeater();
            }
        }
        //Method For Bind Data To Repeater Control
        private void BindRepeater()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.Connection.Open();
            cmd.CommandText = "usp_selectRepeaterDemo";
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            repeater.DataSource = dt;
            repeater.DataBind();
            conn.Close();
        }
        //Method for Clear Text From TextBoxes.
        private void clearcontrol()
        {
            txtname.Text = string.Empty;
            txtpass.Text = string.Empty;
            txtusername.Text = string.Empty;
        }
        protected void OnItemCommand_repeater(object source, RepeaterCommandEventArgs e)
        {
            //Code For Edit Record
            //On Commad Name Supplied to the Image Button-- "Edit" Button
            if (e.CommandName == "Edit")
            {
                //Getting Id of the selected record
                Label lblid1 = (Label)e.Item.FindControl("labelID");
                Label lblName = (Label)e.Item.FindControl("labelname");
                Label lblUsername = (Label)e.Item.FindControl("labelusername");
                Label lblPassword = (Label)e.Item.FindControl("labelpassword");
                userid = Convert.ToInt32(lblid1.Text);
                txtname.Text = lblName.Text;
                txtusername.Text = lblUsername.Text;
                txtpass.Text = lblPassword.Text;
                btnsave.Text = "Update";
            }
            //Code For Delete Record
            //On Commad Name Supplied to the Image Button-- "Delete" Button
            if (e.CommandName == "Delete")
            {
                ImageButton imgbtndelete = (ImageButton)e.Item.FindControl("imgdelete");
                //Getting Id of the selected record
                Label lblid1 = (Label)e.Item.FindControl("labelID");
                userid = Convert.ToInt32(lblid1.Text);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandText = "usp_DeleteRepeaterRec";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@id", userid);
                cmd.ExecuteNonQuery();
                conn.Close();
                BindRepeater();
                Response.Write("Record Deleted SuccessFully!");
            }
            //Code For Insert Record
            //On Commad Name Supplied to the Link Button-- "Insert" Button
            if (e.CommandName == "Insert")
            {
                TextBox txtUname = (TextBox)e.Item.FindControl("txtftrname");
                TextBox txtUusername = (TextBox)e.Item.FindControl("txtftrusername");
                TextBox txtUPassword = (TextBox)e.Item.FindControl("txtftrpassword");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Connection.Open();
                cmd.CommandText = "usp_RepeaterDemo";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", txtUname.Text);
                cmd.Parameters.AddWithValue("@username", txtUusername.Text);
                cmd.Parameters.AddWithValue("@password", txtUPassword.Text);
                Response.Write("Record Inserted Successfully!");
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                BindRepeater();
            }
        }
        //Cancel Button
        protected void btncancel_Click(object sender, EventArgs e)
        {
            //clear Text From Textboxes
            clearcontrol();
            btnsave.Text = "Save";
        }
    }
}
In above Code, BindRepeater() method is used to bind data into database and clearcontrol() method used to Clear Text From textboxes and ”OnItemCommand” for  Insert,Edit Delete Operation. 

OutPut:


2 comments:

  1. t is very good blog and useful for students and developer ,Thanks for sharing
    .Net Online Training
    Dot Net Online Training Bangalore
    .Net Online Course

    ReplyDelete
  2. How to insert record using repeater and paging my website is mcq record storing first page but moving on second page it not saving record else store only second page record mismatch

    ReplyDelete

Popular Posts

Recent Posts

Sample Text

Stats

Powered by Blogger.

Popular Posts

Popular Posts

Join US on Facebook