Export (0) Print
Expand All

Updating Existing Records in a Dataset

In order to edit an existing record in a dataset, you need to access a particular column of data in a particular row. The data in both typed and untyped datasets can be accessed via the following:

  • With the indices of the tables, rows, and columns collections.
  • By passing the table and column names as strings to their respective collections.

Although typed datasets can use the same syntax as untyped datasets, there are additional advantages to using typed datasets. For more information, see the "To update existing records using typed datasets" section below.

To update existing records in either typed or untyped datasets

  • Assign a value to a specific column within a DataRow object.

    The table and column names of untyped datasets are not available at design time and must be accessed through their respective indices.

    The following example shows how to update the data in the first two columns of the fifth record of the first table in dataset1, accessing the data values with the tables, rows, and items (columns) collection indices:

    ' Visual Basic
    dataset1.Tables(0).Rows(4).Item(0) = "Wingtip Toys"
    dataset1.Tables(0).Rows(4).Item(1) = "Buffalo"
    
    // C#
    dataset1.Tables[0].Rows[4].ItemArray[0] = "Wingtip Toys";
    dataset1.Tables[0].Rows[4].ItemArray[1] = "Buffalo";
    

    The following example shows how to update the same data as the preceding example, replacing the collection indices with table, and column names passed as strings:

    ' Visual Basic
    dataset1.Tables("Customers").Rows(4).Item("CompanyName") = "Wingtip Toys"
    dataset1.Tables("Customers").Rows(4).Item("City") = "Buffalo"
    
    // C#
    dataset1.Tables["Customers"].Rows[4]["CompanyName"] = "Wingtip Toys";
    dataset1.Tables["Customers"].Rows[4]["City"] = "Buffalo";
    

To update existing records using typed datasets

  • Assign a value to the specific column within a DataRow object.

    Typed datasets utilize early binding, which exposes the table and column names as properties at design time. This results in code that is both easier to read and write.

    The following example shows how to update the data in the CompanyName and City columns of the fifth record in the Customers table in the DsCustomers1 dataset:

    ' Visual Basic
    DsCustomers1.Customers(4).CompanyName = "Wingtip Toys"
    DsCustomers1.Customers(4).City = "Buffalo"
    
    // C#
    dsCustomers1.Customers[4].CompanyName = "Wingtip Toys";
    dsCustomers1.Customers[4].City = "Buffalo";
    

See Also

Editing Data in a Table | Adding Data to a Table | Deleting a Row from a Table | Updating, Inserting, and Deleting Records in a Dataset | Data Update Events | Suspending Update Constraints | Code: Updating a DataRow with the Value from a Variable (Visual Basic)

Show:
© 2014 Microsoft