question

BenTam-3003 avatar image
0 Votes"
BenTam-3003 asked karenpayneoregon commented

Error: DataTable already belongs to this DataSet.

Dear All,

The red ellipse line causes an error "Datatable already belongs to this DataSet." How to fix it?

195036-error.gif


dotnet-csharp
error.gif (38.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon commented

You will need to make a copy of that table via DataTable.Copy. Once done, use the copy.


· 13
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @karenpayneoregon

How to make a copy? I tried the following syntax. However, "Copy" is redlined. Please help!

195117-error.gif

0 Votes 0 ·
error.gif (7.2 KiB)

Greetings, Copy is a method so use Copy()

0 Votes 0 ·
BenTam-3003 avatar image BenTam-3003 karenpayneoregon ·

Hi @karenpayneoregon

After adding a pair of brackets, it is still redlined.

195212-error.gif


0 Votes 0 ·
error.gif (5.4 KiB)
Show more comments

Here is how to use Copy

 DataSet ds = new DataSet();
 DataView dv = new DataView();
 dv = ds.Tables["StudentList_Table"].DefaultView;
    
 DataTable dataTable = dv.Table;
 DataTable copyTable = dataTable.Copy();
 ds.Clear();
    
 var count = copyTable.Rows.Count;
 Console.WriteLine(count);


195208-f1.png


0 Votes 0 ·
f1.png (31.7 KiB)
BenTam-3003 avatar image BenTam-3003 karenpayneoregon ·

Dear @karenpayneoregon

After copying your code, the redline disappears. However, after pressing the down arrow, the following error still appears.

  A DataTable named 'StudentList_table' already belongs to this DataSet.

195210-error.gif


0 Votes 0 ·
error.gif (18.6 KiB)
Show more comments