Looping through a Datatable.

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

I gota scoot here so I gota make it quick :)

I'm doing a conversion from 2 different databases (2 different software
versions)

I have each table stored in a Datatable, and I'm going to append the columns
from V1 that I need to the V2 table.

After creating the tables (works 100%) i run this code:

DataRow rowV2 = tblItemV2.NewRow();

foreach(DataRow row in tblItemV1)

{

foreach(DataColumn col in row)

{

switch(col.Caption)

{.....

I keep getting errors on the first foreach(DataRow row in tblItemV1)

telling me :

foreach statement cannot operate on variables of type
'System.Data.DataTable' because 'System.Data.DataTable' does not contain a
definition for 'GetEnumerator', or it is inaccessible



Any help on this would be sooo sweet.



Thanks!
 
Hi Ryan,
DataRow rowV2 = tblItemV2.NewRow();

foreach(DataRow row in tblItemV1)
{

It should be
foreach(DataRow row in tblItemV1.Rows)

Your next foreach won't work either:

it should be:

foreach (DataRow row in tblItemV1.Rows)
{
foreach (DataColumn col in tblItemV1.Columns)
{
switch(col.Caption)
{

}
}
}

Cheers

Arne Janning
 
Thanks for the help Arne!
It makes more sense now :)

I usually code all of my stuff in ASP.NET VB, but I am trying this project
in C# for the heck of it.
tblItemV1 = CreateTable("tblItems",strDBV1, tblItemV1);
tblItemV2 = CreateTable("tblItems",strDBV2, tblItemV2);
.....
DataRow rowV2 = tblItemV2.NewRow();

foreach(DataRow row in tblItemV1.Rows)
{
foreach(DataColumn col in tblItemV1.Columns)
{
switch(col.Caption)
{
case "SourceID":
rowV2[col] = row[col];
break;

That's what I'm curently using at the momemt. However, I can't add the
object 'col' to rowV2 because 'col' isn't from that table, but both
tables have the column SourceID. I know I could do this with a for loop,
but there should be a way of doing it this way as well.

Any help or pointers would be great, thanks!!



/RT
 
Solved this part, just used the column name as a string instead of the col
object.

case "SourceID":
rowV2["SourceID"] = row[col];
break;
Thanks for the help Arne!
It makes more sense now :)

I usually code all of my stuff in ASP.NET VB, but I am trying this project
in C# for the heck of it.
tblItemV1 = CreateTable("tblItems",strDBV1, tblItemV1);
tblItemV2 = CreateTable("tblItems",strDBV2, tblItemV2);
....
DataRow rowV2 = tblItemV2.NewRow();

foreach(DataRow row in tblItemV1.Rows)
{
foreach(DataColumn col in tblItemV1.Columns)
{
switch(col.Caption)
{
case "SourceID":
rowV2[col] = row[col];
break;

That's what I'm curently using at the momemt. However, I can't add the
object 'col' to rowV2 because 'col' isn't from that table, but both
tables have the column SourceID. I know I could do this with a for loop,
but there should be a way of doing it this way as well.

Any help or pointers would be great, thanks!!



/RT



Arne Janning said:
Hi Ryan,


It should be
foreach(DataRow row in tblItemV1.Rows)

Your next foreach won't work either:

it should be:

foreach (DataRow row in tblItemV1.Rows)
{
foreach (DataColumn col in tblItemV1.Columns)
{
switch(col.Caption)
{

}
}
}

Cheers

Arne Janning
contain
 
Back
Top