Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A DataTable named 'Table' already belongs to this DataSet

Tags:

c#

asp.net

when i am adding second table(dtResult) to data set that time it's giving error

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

DataTable dtSession = new DataTable();
DataTable dtResult= new DataTable();
dtResult.TableName = "A";
dtSession.TableName = "B";
dtSession = objOpt.GetSearchDetails().Copy();
ds.Tables.Add(dtSession);
dtResult = objOpt.Search_Synchronous().Copy();
ds.Tables.Add(dtResult);

Thanks in advance

like image 477
Chhatrapati Sharma Avatar asked Dec 26 '22 19:12

Chhatrapati Sharma


1 Answers

You need to name the tables after getting the copy from your method and before adding it to the DataSet.

DataTable dtResult= new DataTable();

dtSession = objOpt.GetSearchDetails().Copy();
dtSession.TableName = "B";
ds.Tables.Add(dtSession);


dtResult = objOpt.Search_Synchronous().Copy();
dtResult.TableName = "A";
ds.Tables.Add(dtResult);

Since you are getting the copy from your methods objOpt.GetSearchDetails().Copy() and objOpt.Search_Synchronous().Copy(), they are overwriting the names assigned to the table previously, and both of these are returning the table with name Table, that is why you are getting this error

like image 139
Habib Avatar answered Dec 29 '22 09:12

Habib