Click here to Skip to main content
15,879,047 members
Articles / Web Development / ASP.NET
Tip/Trick

Performing Insert, Update and Delete Operations using DetailsView Control in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
17 Dec 2012CPOL2 min read 54K   2.4K   11   3
This tip describes the working of DetailsView for inserting a new record, updating and deleting the existing details of the user.

Introduction

This tip describes the working of DetailsView for inserting a new record, Updating and Deleting the existing details of the user.

Background

The idea behind using the DetailsView control for displaying, updating and deleting data is because DetailsView control gives the ability to display, edit, insert, or delete a single record at a time from its associated data source. By default, the DetailsView control displays each field of a record on its own line.

The DetailsView control is typically used for updating and inserting new records, often in a master/detail scenario where the selected record of the master control determines the record to display in the DetailsView control. The DetailsView control displays only a single data record at a time, even if its data source exposes multiple records. The DetailsView control does not support sorting but it supports paging.

Using the Code 

To bind the DetailsView control to a data source:

  1. In Design view, right-click the DetailsView control, and then click Show Common Control Tasks.
  2. On the Common DropDownList Tasks menu, click an existing data source or <New Data Source...> in the Choose Data Source dropdown.
  3. If you choose <New Data Source...>, configure a new data source in the Data Source Configuration Wizard.

Now when we have the datasource ready, we can use the OnDataBound event to show the data in the required template in the following manner:

C#
protected void DVUser_DataBound(object sender, EventArgs e)
{
	if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit)
	{
		DataRowView row = (DataRowView)((DetailsView)sender).DataItem;
		TextBox txtUname = (TextBox)((DetailsView)sender).FindControl("txtName");
		RadioButtonList rblGender = 
		    (RadioButtonList)((DetailsView)sender).FindControl("rbGender");
		DropDownList ddlQualification = 
		    (DropDownList)((DetailsView)sender).FindControl("ddlQualification");
		txtUname.Text = row[1].ToString().Trim();
		rblGender.SelectedValue = row[2].ToString().Trim();
		ddlQualification.SelectedValue = row[3].ToString().Trim();
	}
}

Image 1

Now we have seen how to display the record, let us see how we can insert a new record. For doing this, we need to handle the ItemInserting event and then push the new data into the database.

C#
protected void DVUser_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
	try
	{
		TextBox txtUname = (TextBox)((DetailsView)sender).FindControl("txtName");
		RadioButtonList rblGender = 
		   (RadioButtonList)((DetailsView)sender).FindControl("rbGender");
		DropDownList ddlQualification = 
		   (DropDownList)((DetailsView)sender).FindControl("ddlQualification");
		SqlDataSource2.InsertParameters["U_Name"].DefaultValue = txtUname.Text.Trim();
		SqlDataSource2.InsertParameters["Gender"].DefaultValue = 
							rblGender.SelectedValue;
		SqlDataSource2.InsertParameters["Qualification"].DefaultValue = 
						ddlQualification.SelectedValue;
	}
	catch (Exception ex)
	{
		throw new Exception(ex.Message);
	}
}

Image 2

Now we have seen how to show and insert the data, what we need now is to update the item, we need to handle the ItemUpdating event. In this event, we will have to take the new values given by the user and take the required action like putting/updating in database.

C#
protected void DVUser_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
	try
	{
		TextBox txtUname = (TextBox)((DetailsView)sender).FindControl("txtName");
		RadioButtonList rblGender = 
		    (RadioButtonList)((DetailsView)sender).FindControl("rbGender");
		DropDownList ddlQualification = 
		    (DropDownList)((DetailsView)sender).FindControl("ddlQualification");
		SqlDataSource2.UpdateParameters["U_Name"].DefaultValue = txtUname.Text.Trim();
		SqlDataSource2.UpdateParameters["Gender"].DefaultValue = 
							rblGender.SelectedValue;
		SqlDataSource2.UpdateParameters["Qualification"].DefaultValue = 
						ddlQualification.SelectedValue;
	}
	catch (Exception ex)
	{
		throw new Exception(ex.Message);
	}
}

Image 3

Point of Interest

In this tip, we have seen how to perform insert, update and delete operations using DetailsView control in ASP.NET. It is rather easy, but many new developers seem to be struggling with it so I wrote this tip to help them.

History

  • 11th October 2012: First version 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I am a Software Engineer from Bhopal. I started my Career from Programming in ASP and now working as a Web Developer in ASP.Net (C#). I Love coding and always ready to gain new thing and always been towards Microsoft Technologies. Apart from coding my other hobbies are traveling, Internet Surfing, spending time with family and hang out with friends.

http://www.webtekspace.blogspot.in/

Comments and Discussions

 
AnswerDetailsview CRUD Operations Pin
Ranjeet patil30-Aug-14 0:54
Ranjeet patil30-Aug-14 0:54 
Questionfor ~400 words(with code) in the article ~100 are copy pasted from MSDN Pin
Selvin11-Oct-12 5:10
Selvin11-Oct-12 5:10 
AnswerRe: for ~400 words(with code) in the article ~100 are copy pasted from MSDN Pin
AshishChaudha7-Apr-13 17:57
AshishChaudha7-Apr-13 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.