Brettb.Com
  HOME | ABOUT ME | BIOTECHNOLOGY | ARTICLES | GALLERY | CONTACT
Search: Go
TECHNICAL ARTICLES
 ASP
 ASP.NET
 JavaScript
 Transact SQL
 Other Articles
 Software Reviews

PHOTO GALLERIES
 Canon EOS 300D Samples
 Akihabara Maids!
 More Galleries...

TRAVEL LOG
 2009: China
 2008: Tokyo
 2007: Tokyo
 2006: Hong Kong
 2005: New York City

MORE STUFF
 Search Engine Optimisation
 Build an ASP Search Engine
 My Tropical Fishtank
 Autoglass
 SQL Month Name
 SQL Get Date Today
 SQL Year Month
 Other New Stuff...

POPULAR STUFF
 Regular Expressions
 Index Server & ASP
 JavaScript Ad Rotator

Home > ASP.NET Articles

Changing Data in an ADO.NET DataTable Using AcceptChanges Method

This article describes how to use the AcceptChanges method of the DataRow class. The method is invaluable if you need to change data in an ADO.NET DataTable object before displaying it in the web browser. The procedure described here works regardless of where the data in the DataTable object was populated from. In this particular example, the data is modified in a DataTable that is populated from static data, but it can just as easily be used to change data derived from a database.

Creating the ADO.NET data source

In this particular example, the DataTable containing the data to be modified is created and populated within code, but the technique could also be applied to a DataTable populated from a database.

This VB.NET code is used to create the DataTable object called MyDataTable, and populate it with four data columns.

Code Listing 1

'Createadatatable
DimMyDataTableAsNewDataTable("MyDataTable")
MyDataTable.Columns.Add(NewDataColumn("UserID",System.Type.GetType("System.Int32")))
MyDataTable.Columns.Add(NewDataColumn("FirstName",System.Type.GetType("System.String")))
MyDataTable.Columns.Add(NewDataColumn("LastName",System.Type.GetType("System.String")))
MyDataTable.Columns.Add(NewDataColumn("PhoneNumber",System.Type.GetType("System.String")))

Once the DataTable object has been created and its columns have been defined, data rows can be added to the DataTable. In the example code below, a new row is added to the DataTable object MyDataTable.

Code Listing 2

'Populatethedatatablewithadatarow
DimDataRow1AsDataRow=MyDataTable.NewRow()
DataRow1("UserID")=1
DataRow1("FirstName")="Fred"
DataRow1("LastName")="Jones"
DataRow1("PhoneNumber")="5542"
MyDataTable.Rows.Add(DataRow1)

Once the DataTable object has been instantiated and populated with data, the object can be iterated. In the VB.NET sample code below, the row containing the value of "1" for the UserID column has the value of the PhoneNumber column changed to "5643." Calling the AcceptChanges() method on the current DataRow object then causes this change to be made to the row in the MyDataTable DataTable.

Code Listing 3

'Iteratethroughdatatable
DimMyDataRowAsDataRow
ForEachMyDataRowInMyDataTable.Rows
IfConvert.ToInt32(MyDataRow("UserID"))=1Then
MyDataRow("PhoneNumber")="5643"
MyDataRow.AcceptChanges()
EndIf
Next

The updated data can then be displayed in a DataGrid control in the usual way.

Code Listing 4

'Binddatatabletodatagrid
DataGrid1.DataSource=MyDataTable
DataGrid1.DataBind()
  Site Map | Privacy Policy

All content is 1995 - 2012