DataTable.LoadDataRow Method

Definition

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

Overloads

LoadDataRow(Object[], Boolean)

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

LoadDataRow(Object[], LoadOption)

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

LoadDataRow(Object[], Boolean)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public System.Data.DataRow LoadDataRow(object?[] values, bool fAcceptChanges);
C#
public System.Data.DataRow LoadDataRow(object[] values, bool fAcceptChanges);

Parameters

values
Object[]

An array of values used to create the new row.

fAcceptChanges
Boolean

true to accept changes; otherwise false.

Returns

The new DataRow.

Exceptions

The array is larger than the number of columns in the table.

A value doesn't match its respective column type.

Adding the row invalidates a constraint.

Attempting to put a null in a column where AllowDBNull is false.

Examples

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.

C#
using System;
using System.Data;

class MyDataSet {
   public static void Main() {
      DataTable dt = new DataTable();

      DataColumn dc1 = new DataColumn("col1");
      DataColumn dc2 = new DataColumn("col2");
      DataColumn dc3 = new DataColumn("col3");

      dt.Columns.Add(dc1);
      dt.Columns.Add(dc2);
      dt.Columns.Add(dc3);

      // 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 row;

      dt.BeginLoadData();

      // Add the new row to the rows collection.
      row = dt.LoadDataRow(newRow, true);

      foreach (DataRow dr in dt.Rows) {
         Console.WriteLine(String.Format("Row: {0}, {1}, {2}", dr["col1"], dr["col2"], dr["col3"]));
      }

      dt.EndLoadData();
   }
}

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

LoadDataRow(Object[], LoadOption)

Source:
DataTable.cs
Source:
DataTable.cs
Source:
DataTable.cs

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

C#
public System.Data.DataRow LoadDataRow(object?[] values, System.Data.LoadOption loadOption);
C#
public System.Data.DataRow LoadDataRow(object[] values, System.Data.LoadOption loadOption);

Parameters

values
Object[]

An array of values used to create the new row.

loadOption
LoadOption

Used to determine how the array values are applied to the corresponding values in an existing row.

Returns

The new DataRow.

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.

The value of the loadOption parameter is used to determine how the values in the array are applied to an existing row. For example, if loadOption is set to OverwriteChanges, the Original and Current values of each column are replaced with the values in the incoming row and the RowState property is set to Unchanged.

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.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1