how to add datatable to dataset in asp.net

34,825

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.

Share:
34,825
ibrahimkhan
Author by

ibrahimkhan

Updated on May 22, 2020

Comments

  • ibrahimkhan
    ibrahimkhan almost 4 years
    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..