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
-
//Within main class
-
int[] iaInvID = {2,5,8,11};
-
DataSet ds = new DataSet();
-
ds = MyDll.MyDllsClass.ReturnSalesSummary(iaInvID);
-
-
//Within MyDll.MyDllsClass class
-
public static DataSet ReturnSalesSummary(int[] iaInvID)
-
{
-
DataSet ds = new DataSet();
-
-
ds = CreateRelationshipTable(ds, iaInvID);
-
ds.Tables.Add(ReturnSalesSummary_DummySet(iaInvID));
-
-
ds.Relations.Add("InvID",
-
ds.Tables[0].Columns["InvID"],
-
ds.Tables[1].Columns["InvID"]);
-
-
return ds;
-
}
-
-
private static DataSet CreateRelationshipTable(DataSet ds, int[] iaInvID)
-
{
-
//For each id, create a row of that id. This allows a one to many relationship
-
//with the table from the db.
-
DataTable dt = ds.Tables.Add("InvIDs");
-
dt.Columns.Add("ID", typeof(int));
-
foreach (int i in iaInvID)
-
dt.Rows.Add(i);
-
return ds;
-
}
-
-
private static DataTable ReturnSalesSummary_DummySet(int[] iaInvID)
-
{
-
//Until the SQL statement is correct, create a dummy table of similar data.
-
DataSet ds = new DataSet();
-
DataTable dt = ds.Tables.Add();
-
-
dt.Columns.Add("RowID", typeof(int));
-
dt.Columns.Add("InvID", typeof(int));
-
dt.Columns.Add("StationID", typeof(string));
-
-
int iCount = 0;
-
string[] saStationID = { "Workstation 1", "Workstation 2", "Online Sales" };
-
-
foreach (int i in iaInvID)
-
for( int i2 = 0; i2 < 3; i2++)
-
{
-
dt.Rows.Add(iCount, i, saStationID[i2]);
-
iCount++;
-
}
-
-
return ds.Tables[0];
-
}
-