Hi,
I have the following fragment of code which retrieves datarows from a data
table using the Select method.I want to add these rows into another data
table.But I get the Exception:
'Column "ReleaseDate" already belongs to another table'

// Use the Select method to find all rows matching the filter.
DataRow[] foundRows = dt.Select( strExpr, strSort,
DataViewRowState.CurrentRows);
int len = foundRows.Length;

DataColumn dc=new DataColumn();
foreach( DataRow r in foundRows )
{
foreach( DataColumn c in r.Table.Columns )
{
dc=c;
dt1.Columns.Add(c);
ColCnt=r.Table.Columns.IndexOf(c);
}
ii = 1;
}
dr=dt1.NewRow();
dr[ColCnt]=dc;
dt1.Rows.Add(dr);
}*/

--
Message posted via http://www.dotnetmonster.com

Re: Retrieving datarows using DataTable.Select method and adding it to new DataTable by Marina

Marina
Mon Apr 04 11:10:42 CDT 2005

Your problem has nothing to do with Select.

First off, there is no reason to say :dc = c, since it doesn't do a thing
for you. You don't even use the 'dc' variable anywhere. Also, instantiating
before the loop, has no effect, since you are just throwing that object away
when you say "dc = c"

The problem is that, you are adding datacolumns that belong to one table, to
another one. You can't do that, as the message clearly says.

"C.Anand via DotNetMonster.com" <forum@DotNetMonster.com> wrote in message
news:7ea6174f04044c22becc250e0023e6e1@DotNetMonster.com...
> Hi,
> I have the following fragment of code which retrieves datarows from a data
> table using the Select method.I want to add these rows into another data
> table.But I get the Exception:
> 'Column "ReleaseDate" already belongs to another table'
>
> // Use the Select method to find all rows matching the filter.
> DataRow[] foundRows = dt.Select( strExpr, strSort,
> DataViewRowState.CurrentRows);
> int len = foundRows.Length;
>
> DataColumn dc=new DataColumn();
> foreach( DataRow r in foundRows )
> {
> foreach( DataColumn c in r.Table.Columns )
> {
> dc=c;
> dt1.Columns.Add(c);
> ColCnt=r.Table.Columns.IndexOf(c);
> }
> ii = 1;
> }
> dr=dt1.NewRow();
> dr[ColCnt]=dc;
> dt1.Rows.Add(dr);
> }*/
>
> --
> Message posted via http://www.dotnetmonster.com



Re: Retrieving datarows using DataTable.Select method and adding it to new DataTable by C

C
Mon Apr 04 11:15:24 CDT 2005

Sorry,
Here is the code Fragment :
DataRow[] foundRows = dt.Select( strExpr, strSort,
DataViewRowState.CurrentRows);
DataRow dr;
int ColCnt=0;
DataColumn dc=new DataColumn();
foreach( DataRow r in foundRows )
{
foreach( DataColumn c in r.Table.Columns )
{
dc=c;
dt1.Columns.Add(c);
ColCnt=r.Table.Columns.IndexOf(c);
}

dr=dt1.NewRow();
dr[ColCnt]=dc;
dt1.Rows.Add(dr);
}

--
Message posted via http://www.dotnetmonster.com

Re: Retrieving datarows using DataTable.Select method and adding it to new DataTable by C

C
Mon Apr 04 11:19:05 CDT 2005

Should I add the table to a new dataset then?Or alternatively could I use
DataViews concept?

--
Message posted via http://www.dotnetmonster.com

Re: Retrieving datarows using DataTable.Select method and adding it to new DataTable by Cor

Cor
Mon Apr 04 11:21:33 CDT 2005

C,

Normally you can clone your datatable and than use the importrow to get it
from the other table (or your selected collection) in a loop. Even to easy
to show it in code.

I hope this helps,

Cor