This documentation is archived and is not being maintained.

DataTableExtensions.CopyToDataTable(T) Method (IEnumerable(T), DataTable, LoadOption, FillErrorEventHandler)

Copies DataRow objects to the specified DataTable, given an input IEnumerable(T) object where the generic parameter T is DataRow.

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

JScript does not support generic types or methods.

Type Parameters


The type of objects in the source sequence, typically DataRow.


Type: System.Collections.Generic.IEnumerable(T)

The source IEnumerable(T) sequence.

Type: System.Data.DataTable

The destination DataTable.

Type: System.Data.LoadOption

A LoadOption enumeration that specifies the DataTable load options.

Type: System.Data.FillErrorEventHandler

A FillErrorEventHandler delegate that represents the method that will handle an error.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable(T). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


The copied DataRow objects do not fit the schema of the destination DataTable.


The source IEnumerable(T) sequence is a null reference (Nothing in Visual Basic) or the destination DataTable is a null reference (Nothing in Visual Basic).


A DataRow in the source sequence has a state of Deleted.


The source sequence does not contain any DataRow objects.


A DataRow in the source sequence is a null reference (Nothing in Visual Basic).

The parameter T of the input parameter source can only be of type DataRow, or a type derived from DataRow.

The input sequence can be any IEnumerable(T) source, not only a query over a DataTable. Note that if the source sequence is a query, calling this operator will force immediate execution. Enumerating the source sequence can also cause exceptions to be thrown.

The schema of the destination table is based on the schema of the first DataRow row in the source sequence. The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. For a typed DataTable, types are not preserved. The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

When a null reference or nullable type with a value of null is found in a source DataRow, this method replaces the value in the destination DataTable with a value of Value.

The generated table returned by the method has the default table name. If you want to name the DataTable, attach it to a DataSet, or perform any other DataTable specific operation, you must do so after the table is created and returned.

If an exception is thrown during the copy of a data row into the target table, such as a constraint exception, the errorHandler delegate is called. A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

  • The Values property is set to a copy of the source data.

  • The DataTable property is set to the target DataTable.

  • The Errors property is set to the caught exception.

The Continue property is read after the delegate call returns. If the Continue property is true, the source sequence continues to be enumerated and loaded into the data table. If the Continue property is false, the original exception is thrown from the CopyToDataTable method.

For more information, see Creating a DataTable From a Query (LINQ to DataSet).

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5