Adding a new datarow

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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

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
 
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)
 
Back
Top