Home > DeveloperSection > Forums > Insert, Update, Delete in DetailsView in ASP.NET
aditya kumar Patel

Total Post:250

Points:1782
Posted on    September-26-2016 7:08 PM

 ASP.Net ASP.Net  HTML 
Ratings:


 1 Reply(s)
 289  View(s)
Rate this:
I want to add update delete using detailsview in asp.net


aditya kumar Patel

Total Post:250

Points:1782
Posted on    September-26-2016 7:09 PM

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>


Don't want to miss updates? Please click the below button!

Follow MindStick