Tuesday, May 8, 2012

Copy data from one data table to another data table in asp.net

Title:How to copy data from one table to other table in asp.net

Description:

We have two different methods two copy the data form one data table to another data table .First one is to add the rows directly from one to other,second one is using Import Row method.In first we get the copied data into data row array,then it will go thorough for each row and add to another data table

DataRow[] selectedrows = Orders.Select("Customer = 'Bhaskar'");
DataRow AddNewRow;
foreach (DataRow row in selectedrows)
{
AddNewRow = CustomersDetails.NewRow();
AddNewRow["customername"] = row["customer"];
CustomersDetails.Rows.Add(AddNewRow);
}
ImportRow() Method:
By using this method we just import  the rows   into destination data table from source data table.
DataTable Orders;
DataTable Ordersdetails;
DataRow[] Rows =  Orders.Select("city='Hyd'");
foreach (DataRow Row in Rows)
Ordersdetails.ImportRow(Row);

No comments:

Bel