Monday 11 August 2014

Insert Update Delete in Repetar in asp.net...........

First create Table 







Now Design  Web-Form like..











Design file code(.aspx file)


<div>
    <table>
    <tr>
        <td>Name:</td>
        <td><asp:TextBox runat="server" ID="txtname"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Email:</td>
        <td><asp:TextBox runat="server" ID="txtemail"></asp:TextBox></td>
    </tr>
    <tr>
        <td colspan="2" align="center"><asp:Button  runat="server" ID ="btnsubmit"
                Text="Submit" onclick="btnsubmit_Click"/></td>
    </tr>
    </table>
     <asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
        <HeaderTemplate>
            <table>
                <tr>
                    <th align="left" width="250px" class="name">
                    </th>
                    <th align="right" width="250px" class="size">
                    </th>
                </tr>
            </table>
        </HeaderTemplate>
        <ItemTemplate>
            <table>
                <tr>
                    <td align="left" width="250px" class="name">
                    <asp:Label ID="lblname" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
                      
                    </td>
                    <td align="left" width="250px" class="name">
                    <asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email")%>'></asp:Label>
                      
                    </td>
                    <td align="left" width="250px" class="name">
                    <asp:Label ID="lblid" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
                      
                    </td>
                    <td>
                  <asp:LinkButton ID="lnkEdit" runat="server" CommandArgument='<%#Eval("ID") %>' CommandName="edit">Edit</asp:LinkButton>
                  </td>
                   <td>
                  <asp:LinkButton ID="Lnkdelete" runat="server" CommandArgument='<%#Eval("ID") %>' CommandName="Delete" OnClientClick="return confirm('Are u sure u wnat to delete this record?');">Delete</asp:LinkButton>
                  </td>
                 
                </tr>
               
               
            </table>
        </ItemTemplate>
    </asp:Repeater>
    </div>

code Behind (.CS File code)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace repetar1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        static int CuID;
        protected void Page_Load(object sender, EventArgs e)
        {
            fillrepetar();
        }

        private void fillrepetar()
        {
            SqlConnection con = new SqlConnection("Data Source=ARVINDKATARIA\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from namedetail", con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            cmd.ExecuteReader();
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            con.Close();
        }

        protected void btnsubmit_Click(object sender, EventArgs e)
        {
            if (btnsubmit.Text == "Submit")
            {
                SqlConnection con = new SqlConnection("Data Source=ARVINDKATARIA\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO  namedetail(Name,Email)VALUES('" + txtname.Text + "','" + txtemail.Text + "')", con);
                cmd.ExecuteNonQuery();
                con.Close();
            }
            else
            {
                SqlConnection con = new SqlConnection("Data Source=ARVINDKATARIA\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("update namedetail set Name='" + txtname.Text + "' , Email='" + txtemail.Text + "' where id = '" + CuID + "' ", con);
                cmd.ExecuteNonQuery();
                con.Close();
                fillrepetar();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "edit")
            {
                Label l3 = (Label)e.Item.FindControl("lblid");
                CuID = Convert.ToInt32(l3.Text);
                Label l1 = (Label)e.Item.FindControl("lblname");
                txtname.Text = l1.Text;
                Label l2 = (Label)e.Item.FindControl("lblemail");
                txtemail.Text = l2.Text;
                btnsubmit.Text = "Update";
            }
          
            if (e.CommandName == "Delete")
            {
                Label l3 = (Label)e.Item.FindControl("lblid");
                CuID = Convert.ToInt32(l3.Text);
                SqlConnection con = new SqlConnection("Data Source=ARVINDKATARIA\\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("delete from namedetail where ID = '" + CuID + "' ", con);
                cmd.ExecuteNonQuery();
                fillrepetar();
            }
          
        }
    }
}

Show Edit like...

Show Delete Like....

 


Enjoyyyyyyyy......