shalin gajjar
shalin gajjar

Reputation: 684

DataTable already belongs to this DataSet Error working with dataset

i have dataset that fills data from three tables. here i just want to populate data of menu bar from database.

private DataSet GetData()
    {
        using (DataClassesDataContext db = new DataClassesDataContext())
        {
            var query = from m in db.Menus
                        where (m.IsActive == true)
                        select new
                        {
                            MenuID = m.MenuID,
                            MenuName = m.MenuName,
                            MenuLocation = m.MenuLocation
                        };
            DataSet myDataSet = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("MenuID", typeof(int)));
            dt.Columns.Add(new DataColumn("MenuName", typeof(string)));
            dt.Columns.Add(new DataColumn("MenuLocation", typeof(string)));
            foreach (var item in query)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["MenuID"] = item.MenuID.ToString();
                    dr["MenuName"] = item.MenuName.ToString();
                    if (item.MenuLocation != null)
                    {
                        dr["MenuLocation"] = item.MenuLocation.ToString();
                    }
                    dt.Rows.Add(dr);
                }
            }
            myDataSet.Tables.Add(dt);
            var query1 = from c in db.CategoryMenus
                         where (c.IsActive == true)
                         select new
                         {
                             CategoryID = c.CategoryMenuID,
                             CategoryMenuName = c.CategoryMenuName,
                             CategoryMenuLocation = c.CategoryMenuLocation
                         };
            dt.Columns.Add(new DataColumn("CategoryID", typeof(int)));
            dt.Columns.Add(new DataColumn("CategoryMenuName", typeof(string)));
            dt.Columns.Add(new DataColumn("CategoryMenuLocation", typeof(string)));
            foreach (var item in query1)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["CategoryID"] = item.CategoryID.ToString();
                    dr["CategoryMenuName"] = item.CategoryMenuName.ToString();
                    if (item.CategoryMenuLocation != null)
                    {
                        dr["CategoryMenuLocation"] = item.CategoryMenuLocation.ToString();
                    }
                    dt.Rows.Add(dr);
                }
            }
            //Line 80
            myDataSet.Tables.Add(dt);
            var query2 = from s in db.SubCategoryMenus
                         where (s.IsActive == true)
                         select new
                         {
                             SubCategoryID = s.SubCategoryMenuId,
                             SubCategoryMenuName = s.SubCategoryMenuName,
                             SubCategoryMenuLocation = s.SubCategoryMenuLocation
                         };
            dt.Columns.Add(new DataColumn("SubCategoryID", typeof(int)));
            dt.Columns.Add(new DataColumn("SubCategoryMenuName", typeof(string)));
            dt.Columns.Add(new DataColumn("SubCategoryMenuLocation", typeof(string)));
            foreach (var item in query2)
            {
                if (item != null)
                {
                    DataRow dr = dt.NewRow();
                    dr["SubCategoryID"] = item.SubCategoryID.ToString();
                    dr["SubCategoryMenuName"] = item.SubCategoryMenuName.ToString();
                    if (item.SubCategoryMenuLocation != null)
                    {
                        dr["SubCategoryMenuLocation"] = item.SubCategoryMenuLocation.ToString();
                    }
                    dt.Rows.Add(dr);
                }
            }
            myDataSet.Tables.Add(dt);
            return myDataSet;
        }
    }

from the second query while it iterates and add row to this tables then erro occured like:

Server Error in '/EasyWeb' Application.
DataTable already belongs to this DataSet.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: DataTable already belongs to this DataSet.

Source Error:


Line 78:                 }
Line 79:             }
Line 80:             myDataSet.Tables.Add(dt);
Line 81:             var query2 = from s in db.SubCategoryMenus
Line 82:                          where (s.IsActive == true)


Source File: f:\EasyWeb\MenuControl.ascx.cs    Line: 80

Stack Trace:


[ArgumentException: DataTable already belongs to this DataSet.]
   System.Data.DataTableCollection.BaseAdd(DataTable table) +4825888
   System.Data.DataTableCollection.Add(DataTable table) +112
   MenuControl.GetData() in f:\EasyWeb\MenuControl.ascx.cs:80
   MenuControl.Page_Load(Object sender, EventArgs e) in f:\EasyWeb\MenuControl.ascx.cs:20
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Control.LoadRecursive() +141
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Upvotes: 0

Views: 3790

Answers (2)

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28423

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

Like this

dt.TableName = "A";
myDataSet.Tables.Add(dt);

Upvotes: 1

shalin gajjar
shalin gajjar

Reputation: 684

ok i found just need different tables for that. like

Datatable dt=new Datatable("Menu");
DataTable dt1 = new DataTable("Category");
DataTable dt2 = new DataTable("SubCategory");

Upvotes: 0

Related Questions