DataSet from Method returns error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leviwatts
    New Member
    • Apr 2009
    • 5

    DataSet from Method returns error

    Exception Details: System.Argument Exception: 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

    Code:
    //Within main class
    int[] iaInvID = {2,5,8,11};
    DataSet ds = new DataSet();
    ds = MyDll.MyDllsClass.ReturnSalesSummary(iaInvID);
    Code:
    //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];
    }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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)

    Comment

    • leviwatts
      New Member
      • Apr 2009
      • 5

      #3
      Originally posted by Plater
      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)
      Code:
      //Within main class
      ds = Pyramedium_Enterprise_Reporting.Report_TenderSummaries.ReturnSalesSummary(iaInvID).Copy();
      As before, this results in the same error. Am I using .Copy() wrong?

      Comment

      • leviwatts
        New Member
        • Apr 2009
        • 5

        #4
        [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.
        Code:
        ds.Tables.Add(ReturnSalesSummary_DummySet(iaInvID).Copy());
        Thanks Plater.

        Comment

        Working...