Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

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

5.00/5 (3 votes)
17 Dec 2012CPOL2 min read 54.9K   2.4K  
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)