Data Access in Client and Middle-Tier Programming
How to: Add Rows to a DataTable

To add new records into a dataset, a new data row must be created and added to the DataRow collection (Rows) of a DataTable in the dataset. The following procedures show how to create a new row and insert it into a DataTable. Examples are provided for both typed and untyped datasets.

NoteNote:

Applications that use data-bound controls typically get the ability to add new records through the "add new" button on a BindingNavigator Control.

Inserting a New Record into a Typed Dataset

For this example, it is assumed that a dataset named NorthwindDataset has a Customers DataTable and has two columns named CustomerID and CompanyName. Typed datasets expose the column names as properties of the typed DataRow object; in this case the CustomersRow.

To add a new record to a typed dataset

  • Declare a new instance of the typed dataset. In the following example, you declare a new instance of the CustomersRow class, assign it a new row, populate the columns with data, and add the new row to the Customers table's Rows collection:

    Visual Basic
    Dim newCustomersRow As NorthwindDataSet.CustomersRow
    newCustomersRow = NorthwindDataSet1.Customers.NewCustomersRow()
    
    newCustomersRow.CustomerID = "ALFKI"
    newCustomersRow.CompanyName = "Alfreds Futterkiste"
    
    NorthwindDataSet1.Customers.Rows.Add(newCustomersRow)
    
    C#
    NorthwindDataSet.CustomersRow newCustomersRow = 
        northwindDataSet1.Customers.NewCustomersRow();
    
    newCustomersRow.CustomerID = "ALFKI";
    newCustomersRow.CompanyName = "Alfreds Futterkiste";
    
    northwindDataSet1.Customers.Rows.Add(newCustomersRow);
    
Inserting a New Record into an Untyped Dataset

For this example, it is assumed that the untyped dataset has a Customers DataTable that has two columns named CustomerID and CompanyName. Untyped datasets require knowledge of column names or indices when coding. This example uses column names.

To add a record to an untyped dataset

  • Call the NewRow method of a DataTable to create a new, empty row. This new row inherits its column structure from the data table's DataColumnCollection. The following code creates a new row, populates it with data, and adds it to the table's Rows collection.

    Visual Basic
    Dim newCustomersRow As DataRow = DataSet1.Tables("Customers").NewRow()
    
    newCustomersRow("CustomerID") = "ALFKI"
    newCustomersRow("CompanyName") = "Alfreds Futterkiste"
    
    DataSet1.Tables("Customers").Rows.Add(newCustomersRow)
    
    C#
    DataRow newCustomersRow = dataSet1.Tables["Customers"].NewRow();
    
    newCustomersRow["CustomerID"] = "ALFKI";
    newCustomersRow["CompanyName"] = "Alfreds Futterkiste";
    
    dataSet1.Tables["Customers"].Rows.Add(newCustomersRow);
    
See Also

Tasks

Concepts

Reference

Other Resources

Tags :


Community Content

johnzap
Can't duplicate sample

I can't duplicate the C# sample "add a new record to a typed dataset".

I can't find, access or track down the new record method for my "row" class similiar to:

northwindDataSet1.Customers.NewCustomersRow

If a sample was provided it may help. But it's also difficult to find a Northwind db sample for VS2008.

I created the table in sqlexpress. Created the dataset object using the wizard, dragged the table using the Server Explorer, but can find the New...Row() method.

I really need this functionality, any ideas?

Tags :

sensei 2007
I can´t update or add records in any table using dataset
I´m using samples in this section (http://msdn.microsoft.com/en-us/library/5ycd1034.aspx) and it´s not working, someona have any idea about what happen?

can some one send a sample that works?
Tags :

Page view tracker