This documentation is archived and is not being maintained.

DataTable.LoadDataRow Method

Finds and updates a specific row. If no matching row is found, a new row is created using the given values.

[Visual Basic]
Public Function LoadDataRow( _
   ByVal values() As Object, _
   ByVal fAcceptChanges As Boolean _
) As DataRow
[C#]
public DataRow LoadDataRow(
 object[] values,
 bool fAcceptChanges
);
[C++]
public: DataRow* LoadDataRow(
 Object* values __gc[],
 bool fAcceptChanges
);
[JScript]
public function LoadDataRow(
   values : Object[],
 fAcceptChanges : Boolean
) : DataRow;

Parameters

values
An array of values used to create the new row.
fAcceptChanges
true to accept changes; otherwise, false.

Return Value

The new DataRow.

Exceptions

Exception Type Condition
ArgumentException The array is larger than the number of columns in the table.
InvalidCastException A value doesn't match its respective column type.
ConstraintException Adding the row invalidates a constraint.
NoNullAllowedException Attempting to put a null in a column where AllowDBNull is false.

Remarks

The LoadDataRow method takes an array of values and finds the matching value(s) in the primary key column(s).

If a column has a default value, pass a null value in the array to set the default value for that column. Similarly, if a column has its AutoIncrement property set to true, pass a null value in the array to set the automatically generated value for the row.

If the fAcceptChanges parameter is true or not specified, the new data is added and then AcceptChanges is called to accept all changes in the DataTable; if the argument is false, newly added rows are marked as insertions, and changes to existing rows are marked as modifications.

Exceptions can also occur during either a ColumnChanging or RowChanging event. If an exception occurs, the row is not added to the table.

Use LoadDataRow in conjunction with BeginLoadData and EndLoadData.

Example

[Visual Basic, C#, C++] The following example uses the LoadDataRow method to attempt to find a row. If no such row is found, the values are used to create a new row.

[Visual Basic] 
Private Sub MyAddUpdate(ByVal myTable As DataTable)
    ' Create an array for the values.
    Dim newRow(2) As Object
   ' Set the values of the array.
    newRow(0) = "Hello"
    newRow(1) = "World"
    newRow(2) = "two"
    Dim myRow As DataRow
    myTable.BeginLoadData()
    ' Add the new row to the rows collection.
    myRow = myTable.LoadDataRow(newRow, True)
    myTable.EndLoadData()
End Sub

[C#] 
private void MyAddUpdate(DataTable myTable){
    // Create an array for the values.
    object[] newRow = new object[3];
   // Set the values of the array.
    newRow[0] = "Hello";
    newRow[1] = "World";
    newRow[2] = "two";
    DataRow myRow;
    myTable.BeginLoadData();
    // Add the new row to the rows collection.
    myRow = myTable.LoadDataRow(newRow, true);
    myTable.EndLoadData();
}

[C++] 
private:
 void MyAddUpdate(DataTable* myTable){
     // Create an array for the values.
     Object* newRow[] = new Object*[3];
    // Set the values of the array.
     newRow[0] = S"Hello";
     newRow[1] = S"World";
     newRow[2] = S"two";
     DataRow* myRow;
     myTable->BeginLoadData();
     // Add the new row to the rows collection.
     myRow = myTable->LoadDataRow(newRow, true);
     myTable->EndLoadData();
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataTable Class | DataTable Members | System.Data Namespace

Show: