Hi everybody
I am trying to take an existing datarow from a datatable, modify its Primary Key value
and reinsert into the datatable as a NEW (datarowstate.added) row. The code below i
the track I have been pursuing, but I keep getting the error that the datarow already belong
to another table. This is despite the fact that I essentially murder the temporary datatable I create to
modify the datarow. Any suggestions

'Create new table and import current dataro
Dim dTable As New dsPatients.dtPatientsDataTabl
dTable.ImportRow(DsPatients1.dtPatients(0)

'Reset PtID to negativ
dTable(0).PtID = -

Dim dRowNew As DataRow = dTable(0
'Get rid of old tabl
dTable.Clear(
dTable.AcceptChanges(
dTable.Dispose(
dTable = Nothin

'Try to add modified row back t
'original table as a new ro
DsPatients1.dtPatients.AdddtPatientsRow(dRowNew

JT

Re: Adding a new datarow by Miha

Miha
Thu Feb 05 11:40:41 CST 2004

Hi,

Instead of ImportRow you might use LoadDataRow and pass it array you get
from original row (DataRow.ItemArray).
Something like:
dim rowArray as object[] = DsPatients1.dtPatients(0).ItemArray
rowArray(0) = newPkId
dTable.LoadDataRow(rowArray)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"JTnospam@verizon.net" <anonymous@discussions.microsoft.com> wrote in
message news:AF481A2F-535B-40C9-A73E-5EA2224ADC40@microsoft.com...
> Hi everybody,
> I am trying to take an existing datarow from a datatable, modify its
Primary Key value,
> and reinsert into the datatable as a NEW (datarowstate.added) row. The
code below is
> the track I have been pursuing, but I keep getting the error that the
datarow already belongs
> to another table. This is despite the fact that I essentially murder the
temporary datatable I create to
> modify the datarow. Any suggestions?
>
> 'Create new table and import current datarow
> Dim dTable As New dsPatients.dtPatientsDataTable
> dTable.ImportRow(DsPatients1.dtPatients(0))
>
> 'Reset PtID to negative
> dTable(0).PtID = -1
>
> Dim dRowNew As DataRow = dTable(0)
> 'Get rid of old table
> dTable.Clear()
> dTable.AcceptChanges()
> dTable.Dispose()
> dTable = Nothing
>
> 'Try to add modified row back to
> 'original table as a new row
> DsPatients1.dtPatients.AdddtPatientsRow(dRowNew)
>
> JT



Re: Adding a new datarow by anonymous

anonymous
Thu Feb 05 12:16:09 CST 2004

Miha,
Thank you for your wonderful idea. It lets me do what I want in three lines of code
Thanks again
J

'Create array and import current dataro
Dim rowArray() As Object = DsPatients1.dtPatients(0).ItemArra

'Reset PtID to negativ
rowArray(0) = -

'Add modified row back t
'original table as a new ro
DsPatients1.dtPatients.LoadDataRow(rowArray, False)