Copying records from datatable to datatable in dataset

T

tshad

Running on VS.net 2005,

I am trying to copy rows from my datatable to another datatable in the same
dataset.

The schema would be identical.

I need to make the table name "forms" as I am passing this dataset to
function that expects a dataset with a datatable in it.

I am loading up the dataset with my database information and then copying
the data row by row to the new datatable until the key changes then I would
pass the dataset to the function for processing. When done, I would clear
the datatable (not sure the best way to do this - but was looking as
Clear()).

I am getting errors trying to clone the table so that the schema will be the
same but I can't seem to get it to work. I get an error trying to clone to
may already set up table:

Property or indexer 'System.Data.DataTableCollection.this[string]'
cannot be assigned to -- it is read only

***************************************
ds.Tables.Add("Forms");
ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <---- Error here

foreach (DataRow dr in ds.Tables["UnUsed"].Rows)
{
if (oldApsID == 0)
oldApsID = (int)dr["AppsID"];

if (oldApsID != (int)dr["AppsID"])
{
oldApsID = (int)dr["AppsID"];
ds.Tables["Tables"].Clear();
ds.Tables.Add("Forms");
ds.Tables["Forms"] = ds.Tables["UnUsed"].Clone; <--- same error
here
}

if(oldApsID == (int)dr["ApsID"])
{
ds.Tables["Forms"].ImportRow (dr);
}
}
*****************************************

I assume my syntax is incorrect, or you have to clone and create the table
at the same time.

But I can't seem to get it to work.

Thanks,

Tom
 
T

tshad

Actually, Clone may not be what I am looking for.

What I want to do is create a table in my dataset this has the same columns
as my other table and then copy selected rows into the table (until a value
changes), then clear the table and do it again until I finish with the
original table.

Thanks,

Tom
 

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