Wednesday, March 7, 2012

Row already belongs to another table.

I have a DataGrid that uses a DataTable as a DataSource. Call this table dt.
The DataGrid is editable so changes will be flushed back to the DataSource
(dt). I want to keep track of the changes so I created a duplicate table
(call it dtCopy). To see the changes I simply check every column in every ro
w
and find what has change. In theory this seems like it will work but I run
into a snag when adding a row. I get an exception from SQL indicating that
this row already belongs to another table. This I already know since I am
adding the row to both tables. Any ideas on how I can add a row to both
tables without this error/exception? Any better solution?
Thank you.On Thu, 15 Jun 2006 15:53:01 -0700, Kevin Burton
<KevinBurton@.discussions.microsoft.com> wrote:

>I get an exception from SQL indicating that
>this row already belongs to another table.
The only way I can imagine getting a message about a different table
than the INSERT target is if there is an INSERT TRIGGER on the target
table. You have not mentioned any such trigger.
Posting the exact message would probably be a good idea.
Roy Harvey
Beacon Falls, CT|||Actually, if it's the error that I'm thinking of, it's not a SQL error.
DataSets and Datatables are part of ADO.NET, not the data storage
layer.
Please post the error, but I think you'll be better off looking for
help in a newsgroup for ADO.NET.
Stu
Kevin Burton wrote:
> I have a DataGrid that uses a DataTable as a DataSource. Call this table d
t.
> The DataGrid is editable so changes will be flushed back to the DataSource
> (dt). I want to keep track of the changes so I created a duplicate table
> (call it dtCopy). To see the changes I simply check every column in every
row
> and find what has change. In theory this seems like it will work but I run
> into a snag when adding a row. I get an exception from SQL indicating that
> this row already belongs to another table. This I already know since I am
> adding the row to both tables. Any ideas on how I can add a row to both
> tables without this error/exception? Any better solution?
> Thank you.|||When you copy a row from one datatable to another, it is just referenced.
Thats why you are getting this error.
say you want to copy the rows from tbl1 to another datatable tbl2, then use
the itemarray method.. goes like this.. the exact syntax though you should b
e
checking :)
for(int i = 0;t < tbl1.Rows.Count;i++)
begin
tbl2.Rows.Add(tbl1.Rows(i).ItemArray)
end
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/

No comments:

Post a Comment