Export (0) Print
Expand All

DataTable.LoadDataRow Method (Object(), Boolean)

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

Namespace:  System.Data
Assembly:  System.Data (in System.Data.dll)

'Declaration
Public Function LoadDataRow ( _
	values As Object(), _
	fAcceptChanges As Boolean _
) As DataRow

Parameters

values
Type: System.Object()

An array of values used to create the new row.

fAcceptChanges
Type: System.Boolean

true to accept changes; otherwise false.

Return Value

Type: System.Data.DataRow
The new DataRow.

ExceptionCondition
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.

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.

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.

Imports System.Data

Class MyDataSet
   Public Shared Sub Main()
      Dim dt As New DataTable()

      Dim dc1 As New DataColumn("col1")
      Dim dc2 As New DataColumn("col2")
      Dim dc3 As New DataColumn("col3")

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

      ' Create an array for the values. 
      Dim newRow As Object() = New Object(2) {}

      ' Set the values of the array.
      newRow(0) = "Hello"
      newRow(1) = "World"
      newRow(2) = "two" 
      Dim row As DataRow

      dt.BeginLoadData()

      ' Add the new row to the rows collection.
      row = dt.LoadDataRow(newRow, True)

      For Each dr As DataRow In dt.Rows
         Console.WriteLine([String].Format("Row: {0}, {1}, {2}", dr("col1"), dr("col2"), dr("col3")))
      Next

      dt.EndLoadData()
   End Sub 
End Class

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft