Click here to Skip to main content
Click here to Skip to main content

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

, 17 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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();
	}
}

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.

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);
	}
}

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.

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);
	}
}

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)

Share

About the Author

AshishChaudha
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/
Follow on   Twitter   Google+

Comments and Discussions

 
AnswerDetailsview CRUD Operations PinmemberRanjeet patil30-Aug-14 1:54 
Questionfor ~400 words(with code) in the article ~100 are copy pasted from MSDN PinmemberSelvin11-Oct-12 6:10 
AnswerRe: for ~400 words(with code) in the article ~100 are copy pasted from MSDN PinprofessionalAshishChaudha7-Apr-13 18:57 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Dec 2012
Article Copyright 2012 by AshishChaudha
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid