Insert, Update And Delete Rows In ASP.NET DetailsView Control

Introduction

DetailsView is a DataBound control of ASP.NET used to display a single record from a DataSource in the form of a table, where each row represents a field of the record. The DetailsView control allows you to insert, edit and delete records.

Building the Sample

  1. Download my Sample.
  2. Change Connection String in web.config file. 
    1. <connectionStrings>   
    2.     <add name="MyConnection"   
    3.          connectionString="Data Source=MONISH-PC\MONISH;Initial Catalog=master;Persist Security Info=True;User ID=saty;Password=1234"   
    4.          providerName="System.Data.SqlClient" />   
    5.   </connectionStrings> 
  1. Execute the product table in SQL server. 
    1. USE [master]   
    2. GO   
    3.    
    4. /****** Object:  Table [dbo].[Product]    Script Date: 08/16/2012 19:46:27 ******/   
    5. SET ANSI_NULLS ON   
    6. GO   
    7.    
    8. SET QUOTED_IDENTIFIER ON   
    9. GO   
    10.    
    11. SET ANSI_PADDING ON   
    12. GO   
    13.    
    14. CREATE TABLE [dbo].[Product](   
    15.     [pk_id] [int] IDENTITY(1,1) NOT NULL,   
    16.     [ProductId] [varchar](5) NULL,   
    17.     [ProductName] [varchar](50) NULL,   
    18.     [ProductPrice] [numeric](8, 2) NULL,   
    19.     [ProductLocation] [varchar](25) NULL,   
    20.     [ProductQuantity] [varchar](10) NULL,   
    21. PRIMARY KEY CLUSTERED    
    22. (   
    23.     [pk_id] ASC   
    24. )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ONON [PRIMARY]   
    25. ON [PRIMARY]   
    26.    
    27. GO   
    28.    
    29. SET ANSI_PADDING OFF   
    30. GO   
  1. Build the application and it will work fine.

Description

Initially, I have loaded data from a database to details view, each record is displayed in the details view of each page. After the user clicks the update button to edit and update selected record details, if he or she clicks the Add Row Button then they will be able to add a new record to the database.

AllowPaging >>> true/false

To check whether the DetailsView should support navigation.

ASP.NET

 

Add Record in DetailsView

Add Record in DetailsView

 

Update Record  Details View

Record in DetailsView

 

Delete Record  DetailsView

Record in DetailsView

 

 

Source Code Files

  • SQL

More Information

DetailsView Events I used in this sample:

  • PageIndexChanging
    This event fires when the DetailsView moves to another record. The first one fires before and the second one fires after a page is changed.

  • ItemUpdating
    This event fires when an item is updated. The first one fires before and the second one fires after the record is updated.

  • ItemDeleting
    This event fires when the current record is deleted. The first one fires before and other one fires after the record is deleted.