Connecting Tech Pros Worldwide Help | Site Map

DataSet from Method returns error

Newbie
 
Join Date: Apr 2009
Posts: 5
#1: Apr 6 '09
Exception Details: System.ArgumentException: DataTable already belongs to another DataSet.
Googling for this error shows several post of people trying to manipulate a table within a dataset. Others suggest using .clone() and .copy(). Neither results in a different error.

Within another class, I've gathered two tables of data for a nested repeater. To return a table, int, string or anything else in C#, one sets their local variable equal to the returned data of the method. Doing this with a DataSet provides the above error.

Q: How do I pull a DataSet returned from a method equal to a local DataSet variable?

It is a web site, though I believe this to be a C# syntax issue. Let me know if I'm wrong.

OS: Windows XP SP3
MS VS 05 v8.0.50727.42
.NET 2.0.50727

Expand|Select|Wrap|Line Numbers
  1. //Within main class
  2. int[] iaInvID = {2,5,8,11};
  3. DataSet ds = new DataSet();
  4. ds = MyDll.MyDllsClass.ReturnSalesSummary(iaInvID);
  5.  
Expand|Select|Wrap|Line Numbers
  1. //Within MyDll.MyDllsClass class
  2. public static DataSet ReturnSalesSummary(int[] iaInvID)
  3. {
  4.     DataSet ds = new DataSet();
  5.  
  6.     ds = CreateRelationshipTable(ds, iaInvID);
  7.     ds.Tables.Add(ReturnSalesSummary_DummySet(iaInvID));
  8.  
  9.     ds.Relations.Add("InvID",
  10.     ds.Tables[0].Columns["InvID"],
  11.     ds.Tables[1].Columns["InvID"]);
  12.  
  13.     return ds;
  14. }
  15.  
  16. private static DataSet CreateRelationshipTable(DataSet ds, int[] iaInvID)
  17. {
  18.     //For each id, create a row of that id.  This allows a one to many relationship
  19.     //with the table from the db.
  20.     DataTable dt = ds.Tables.Add("InvIDs");
  21.     dt.Columns.Add("ID", typeof(int));
  22.     foreach (int i in iaInvID)
  23.     dt.Rows.Add(i);
  24.     return ds;
  25. }
  26.  
  27. private static DataTable ReturnSalesSummary_DummySet(int[] iaInvID)
  28. {
  29.     //Until the SQL statement is correct, create a dummy table of similar data.
  30.     DataSet ds = new DataSet();
  31.     DataTable dt = ds.Tables.Add();
  32.  
  33.     dt.Columns.Add("RowID", typeof(int));
  34.     dt.Columns.Add("InvID", typeof(int));
  35.     dt.Columns.Add("StationID", typeof(string));
  36.  
  37.     int iCount = 0;
  38.     string[] saStationID = { "Workstation 1", "Workstation 2", "Online Sales" };
  39.  
  40.     foreach (int i in iaInvID)
  41.     for( int i2 = 0; i2 < 3; i2++)
  42.     {
  43.         dt.Rows.Add(iCount, i, saStationID[i2]);
  44.         iCount++;
  45.     }
  46.  
  47.     return ds.Tables[0];
  48. }
  49.  
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,150
#2: Apr 6 '09

re: DataSet from Method returns error


I think you need to use the .Copy() method to make a copy of the DataTable object if you want to insert it into another dataset. (Just like you would with DataRows)
Newbie
 
Join Date: Apr 2009
Posts: 5
#3: Apr 6 '09

re: DataSet from Method returns error


Quote:

Originally Posted by Plater View Post

I think you need to use the .Copy() method to make a copy of the DataTable object if you want to insert it into another dataset. (Just like you would with DataRows)

Expand|Select|Wrap|Line Numbers
  1. //Within main class
  2. ds = Pyramedium_Enterprise_Reporting.Report_TenderSummaries.ReturnSalesSummary(iaInvID).Copy();
As before, this results in the same error. Am I using .Copy() wrong?
Newbie
 
Join Date: Apr 2009
Posts: 5
#4: Apr 6 '09

re: DataSet from Method returns error


[Edit limit expired]
The error pointed out the method at line 4 of the first set of code, making me assume that was the line causing the issue. Using .Copy() in line 7 of the second set of code pushes me to a new error message.
Expand|Select|Wrap|Line Numbers
  1. ds.Tables.Add(ReturnSalesSummary_DummySet(iaInvID).Copy());
Thanks Plater.
Reply

Tags
dataset, return