我有数据集来填充三个表中的数据。在这里,我只想从数据库中填充菜单栏的数据。
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;
}
}
从第二个查询迭代并向该表添加行然后出现错误,如:
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
答案 0 :(得分:0)
好吧我发现只需要不同的表格。 喜欢
Datatable dt=new Datatable("Menu");
DataTable dt1 = new DataTable("Category");
DataTable dt2 = new DataTable("SubCategory");
答案 1 :(得分:0)
在从方法中获取副本并将其添加到DataSet之前,需要对表进行命名。
喜欢这个
dt.TableName = "A";
myDataSet.Tables.Add(dt);