INSERT, UPDATE, DELETE IN DETAILSVIEW IN ASP.NET

aditya kumar Patel

Total Post:254

Points:1810
Posted by  aditya kumar Patel
 694  View(s)
Ratings:
Rate this:
I want to add update delete using detailsview in asp.net
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: Insert, Update, Delete in DetailsView in ASP.NET

    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.Data;
    using System.Configuration;
    
    namespace testing_all_controls
    {
        public partial class DetailsView : System.Web.UI.Page
        {
            SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    FillGrid();
                }
            }
            private void FillGrid()
            {
                string sqlrt = "";
                sqlrt = "Select id,Name ,Age from test";
                cn.Open();
                SqlDataAdapter da = new SqlDataAdapter();
                da = new SqlDataAdapter(sqlrt, cn);
                DataSet dt = new DataSet();
                da.Fill(dt);
                if (dt.Tables[0].Rows.Count == 0)
                {
                    DataRow dr = dt.Tables[0].NewRow();
                    dr["Name"] = "No Record";
                    dr["Age"] = "No Record";              
    
                    dt.Tables[0].Rows.Add(dr);
    
                }
    
                dtlview.DataSource = dt;
                dtlview.DataBind();
                cn.Close();
            }
    
            protected void dtlview_ItemCommand(object sender, DetailsViewCommandEventArgs e)
            {
                if (e.CommandName == "New")
                {
                   
                    dtlview.ChangeMode(DetailsViewMode.Insert);
                }
                if (e.CommandName == "Cancel")
                {
                    dtlview.ChangeMode(DetailsViewMode.ReadOnly);
                    FillGrid();
                }
                if (e.CommandName == "Change")
                {
                    dtlview.ChangeMode(DetailsViewMode.Edit);
                    FillGrid();
                }
                if (e.CommandName == "Ensert")
                {
                    TextBox txtFirstName = (TextBox)dtlview.FindControl("txtinname");
                    TextBox txtage = (TextBox)dtlview.FindControl("txtinage");
                    SqlCommand cmd = new SqlCommand();
                    cn.Open();
                    cmd.Connection = cn;
                    cmd.CommandText = "insert into test(name,age,id) values('" + txtFirstName.Text + "','" + txtage.Text + "','" + Guid.NewGuid().ToString().Substring(1, 6) + "')";
                    cmd.ExecuteNonQuery();
                    cn.Close();
                    dtlview.ChangeMode(DetailsViewMode.ReadOnly);
                    FillGrid();
                }
    
                if (e.CommandName == "Save")
                {
                    DataKey key = dtlview.DataKey;
                    TextBox txtFirstName = (TextBox)dtlview.FindControl("txtname");
                    TextBox txtage = (TextBox)dtlview.FindControl("txtage");
                    SqlCommand cmd = new SqlCommand();
    
                    cn.Open();
                    cmd.Connection = cn;
                    string sqlrt = "UPDATE test SET name ='" + txtFirstName.Text + "',age ='" + txtage.Text + "' WHERE id='" + key.Value.ToString() + "'";
                    cmd.CommandText = sqlrt;
                    cmd.ExecuteNonQuery();
                    cn.Close();
                    dtlview.ChangeMode(DetailsViewMode.ReadOnly);
                    FillGrid();
    
                   
                }
    
                if (e.CommandName == "Remove")
                {
                    DataKey key = dtlview.DataKey;
                    SqlCommand cmd = new SqlCommand();
                    cn.Open();
                    cmd.Connection = cn;
                    string sqlrt = "delete from test where id='" + key.Value.ToString() + "'";
                    cmd.CommandText = sqlrt;
                    cmd.ExecuteNonQuery();
                    cn.Close();
                    FillGrid();
                }
            }
    
            protected void dtlview_ModeChanging(object sender, DetailsViewModeEventArgs e)
            {
                dtlview.ChangeMode((DetailsViewMode)e.NewMode);
            }
        }
    }
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DetailsView.aspx.cs" Inherits="testing_all_controls.DetailsView" %>
    
    <!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div align="center">
        
            <asp:DetailsView ID="dtlview" runat="server" Height="50px" Width="224px" 
                AutoGenerateRows="False" DataKeyNames="id" 
                onitemcommand="dtlview_ItemCommand" onmodechanging="dtlview_ModeChanging">
            <Fields>
            <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
             <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
            </ItemTemplate>        
            <EditItemTemplate>
                <asp:TextBox ID="txtname" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
            <asp:TextBox ID="txtinname" runat="server" Text='<%# Eval("Name") %>'></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="Age">
            <ItemTemplate>
            <asp:Label ID="Label2" runat="server" Text='<%# Eval("Age") %>'></asp:Label>
            </ItemTemplate>
             <EditItemTemplate>
                <asp:TextBox ID="txtage" runat="server" Text='<%# Eval("Age") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
             <asp:TextBox ID="txtinage" runat="server" Text='<%# Eval("Age") %>'></asp:TextBox>
            </InsertItemTemplate>
            </asp:TemplateField>  
             <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="New" CommandName="New" />   
                 <asp:Button ID="Button2" runat="server" Text="Edit" CommandName="Change" />   
                  <asp:Button ID="Button3" runat="server" Text="Delete" CommandName="Remove" />   
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Button ID="Button4" runat="server" Text="Save" CommandName="Save" />
                <asp:Button ID="Button5" runat="server" Text="Cancel" CommandName="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:Button ID="Button6" runat="server" Text="Ensert" CommandName="Ensert" />
                <asp:Button ID="Button7" runat="server" Text="Cancel" CommandName="Cancel" />
            </InsertItemTemplate>
            </asp:TemplateField>         
            
                  
            </Fields>
            </asp:DetailsView>
        
        </div>
        </form>
    </body>
    </html>
    

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!