Copy of rows from one DataSet to another

  • Thread starter Jesper Stocholm
  • Start date
J

Jesper Stocholm

I have created a method that basically copies rows from one DataSet to
another - the slight problem is just, that it doesn't work exactly as I
thought,

The method looks as this:

private void ConvertDataSetToHashTable(DataSet dataSet_)
{
// m_packageLink is a Hashtable
DataSet newSet = new DataSet();
DataTable table = new DataTable();

long packageId = 0;
int counter = 0;
int counterRows = 0;
foreach (System.Data.DataRow row in dataSet_.Tables[0].Rows)
{
if (!row[11].Equals(DBNull.Value))
{
long pid = Convert.ToInt64(row[11]);
if (pid != packageId)
{
if (counter != 0)
{
newSet.Tables.Add(table);
### DataRow newRow = newSet.Tables[0].Rows[counterRows];
counterRows++;
m_packageLink.Add(packageId,newSet);
}
newSet = new DataSet();
table = new DataTable();
table.ImportRow(row);
packageId = Convert.ToInt64(row[11]);
}
else
{
table.ImportRow(row);
packageId = Convert.ToInt64(row[11]);
}
}
else
{
this.m_packageLink.Add(packageId,table);
dataTable table = new DataTable();
return;
}
counter++;
}
}

The idea is that it runs through the rows in my "mother dataset" and
checks the value of a field. It then adds the rows to another dataset
that is put into a Hashtable for quick access later on.

The problem is that the rows I copy into my new dataset all are empty -
but the rows I copy _from_ have 30 fields in them. If I check the row in
the line marked with ###, the ItemArray of the row is of length 0 - where
it should be 30.

Can any of you guys see, what I am doing wrong?

Thanks,

:blush:)
 
C

Cor

Hi Jesper,

I think that the problem is that your "table" has no schema. (The most
important, it has no columns). You can do that by adding the columns to the
"table" or by "cloning" the schema from the original table.

table=dataSet_.Tables[0].Clone();


I hope this helps,

Cor
 
G

Guest

Hi

The problem you are facing is because the datatable doesn't have any schema associated with it. You have to import the schema into the new dataset using the Clone() method

table = dataSet_.Tables[0].Clone(

Check this link, which describes how to copy rows between datasets

http://msdn.microsoft.com/library/d...s/cpguide/html/cpconcopyingdatasetcontents.as

Hope this helps

Regards
Madh

MVP | MCSD.NE


----- Jesper Stocholm wrote: ----

I have created a method that basically copies rows from one DataSet to
another - the slight problem is just, that it doesn't work exactly as I
thought

The method looks as this

private void ConvertDataSetToHashTable(DataSet dataSet_)

// m_packageLink is a Hashtabl
DataSet newSet = new DataSet()
DataTable table = new DataTable()

long packageId = 0
int counter = 0
int counterRows = 0
foreach (System.Data.DataRow row in dataSet_.Tables[0].Rows)

if (!row[11].Equals(DBNull.Value))

long pid = Convert.ToInt64(row[11])
if (pid != packageId)

if (counter != 0)

newSet.Tables.Add(table)
### DataRow newRow = newSet.Tables[0].Rows[counterRows]
counterRows++
m_packageLink.Add(packageId,newSet)

newSet = new DataSet()
table = new DataTable()
table.ImportRow(row)
packageId = Convert.ToInt64(row[11])

else

table.ImportRow(row)
packageId = Convert.ToInt64(row[11])


else

this.m_packageLink.Add(packageId,table)
dataTable table = new DataTable()
return

counter++



The idea is that it runs through the rows in my "mother dataset" and
checks the value of a field. It then adds the rows to another dataset
that is put into a Hashtable for quick access later on

The problem is that the rows I copy into my new dataset all are empty -
but the rows I copy _from_ have 30 fields in them. If I check the row in
the line marked with ###, the ItemArray of the row is of length 0 - where
it should be 30

Can any of you guys see, what I am doing wrong

Thanks

:blush:
 

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