ibrahimkhan
ibrahimkhan

Reputation: 169

how to add datatable to dataset in asp.net

DataSet ds = new DataSet();
DataRow[] foundRows;
foundRows = ds.Tables[0].Select("MerchantName LIKE '%'", "MerchantName");

DataTable DataTable2 = new DataTable();
DataTable2 = ds.Tables[0].Clone();
foreach (DataRow dr in foundRows)
{
    DataTable2.ImportRow(dr);
}
ds.tables[0].rows.add(DataTable2); // error table already exists.
Loadimages(ds);

hi all, upto foreach loop everything is working fine. In loadimages method i have to dataset. but i have the data in datatable. If i add datatable to dataset i am getting the error saying table already exists. Please help me regarding this.

Thanks in advance..

Upvotes: 3

Views: 35416

Answers (1)

djdd87
djdd87

Reputation: 68466

I don't understand why you're trying to add a DataTable into another DataTable. Surely your code should be as follows:

DataSet ds = new DataSet();
DataRow[] foundRows;
foundRows = ds.Tables[0].Select("MerchantName LIKE '%'", "MerchantName");

DataTable DataTable2 = new DataTable();
DataTable2 = ds.Tables[0].Clone();
DataTable2.TableName = "DataTable2";
foreach (DataRow dr in foundRows)
{
    DataTable2.ImportRow(dr);
}
ds.Tables.Add(DataTable2); 
Loadimages(ds);

The reason for your error is because your DataTables within the DataSet must have unique names.

Upvotes: 11

Related Questions