Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method returning a datatable. I want to assign this datatable to an existing datatable on a dataset(.xsd file).On dataset i have datatable A and datatable B.When i am calling the method the returned datatable i want to assign to datatable A on dataset.I am using this dataset as a report datasource.how can i do this..?Please help me...
Posted
Updated 19-Oct-12 5:07am
v2

Edited:
C#
DataTable dt = SomeMethodReturnDataTable();
YourDataSet ds = new YourDataSet();

Add DataTable into DataSet:
C#
ds.Tables.Add(dt)

or If the columns inside ds.Tables["A"] is same with dt, then you can loop each rows and columns and load the value from dt into ds.Tables["A"].
C#
int c = -1;
foreach (DataRow dr in dt.Rows)
{
    c++;
    ds.Tables["A"].Rows.Add();
    foreach (DataColumn dc in dt.Columns)
    {
        ds.Tables["A"].Rows[c][dc.ColumnName] = dr[dc.ColumnName];
    }
}
 
Share this answer
 
v3
Comments
Saneesh PK 22-Oct-12 9:07am    
Hi Adriancs thanks for the reply...This is what i wanted.But when i am assigning "ds.Tables["A"] = dt" , iam getting the following error
Property or indexer 'System.Data.DataTableCollection.this[string]' cannot be assigned to -- it is read only
adriancs 22-Oct-12 9:49am    
opps, a mistake. I have updated the answer.
Saneesh PK 23-Oct-12 1:06am    
Thanks alot Adrian....it works..
Didnt got your question exactly, please elaborate more.
 
Share this answer
 
Comments
Devang Vaja 22-Oct-12 9:42am    
this is not sollution it is comment boss

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900