on merging datasets and datatables

P

Phil Townsend

I am working in a web app that contains two datasets that are cached. I
would like to pull the first table from each dataset [0] and put each
into a single dataset, so as to have a single dataset with the
following:

DataSet ds=new DataSet();
DataTable dtroles=((DataSet)Cache["fullrolelist"].Tables[0]);
DataTable dtsub=((DataSet)Cache["subcommittees"].Tables[0]);
ds.Tables.Add(dtroles);
ds.Tables.Add(dtsub);

When I try this I get an error saying that these data tables already
belong to another dataset. Is there a way to extract a table from a
dataset and place it into another dataset? Am I approacing this the
right way? Thanks!
 
K

Kai Brinkmann [MSFT]

Your code doesn't work because you are simply creating another reference to
an existing DataTable object; you are not actually creating a new data
table. To do that, you need to use the Clone() method:

DataSet ds = new DataSet();
DataTable dtroles = ((DataSet)Cache["fullrolelist"]).Tables[0].Clone();
DataTable dtsub = ((DataSet)Cache["subcommittees"]).Tables[0].Clone();
ds.Tables.Add(dtroles);
ds.Tables.Add(dtsub);

Hope that helps.

--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Browne

Phil Townsend said:
I am working in a web app that contains two datasets that are cached. I
would like to pull the first table from each dataset [0] and put each
into a single dataset, so as to have a single dataset with the
following:

DataSet ds=new DataSet();
DataTable dtroles=((DataSet)Cache["fullrolelist"].Tables[0]);
DataTable dtsub=((DataSet)Cache["subcommittees"].Tables[0]);


Re: on MERGing ...

ds.Merge(dtroles)
ds.Merge(dtsub)

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top