当前位置: 代码迷 >> ASP.NET >> 急名为“XX”的列已属于此 DataTable,如何解决
  详细解决方案

急名为“XX”的列已属于此 DataTable,如何解决

热度:8429   发布时间:2013-02-25 00:00:00.0
急!!!名为“XX”的列已属于此 DataTable,怎么解决?
求助各位,这个怎么解决,过几天就要交付了
页面报错:
名为“Admin_ID”的列已属于此 DataTable。

说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Data.DuplicateNameException: 名为“Admin_ID”的列已属于此 DataTable。

源错误: 


行 41: SqlDataReader dr = admin.GetAdmins();
行 42: 
行 43: DataTable dt = SQLHelper.ConvertDrToDt(dr);
行 44: dt.Columns.Add("Admin_ID");//
行 45: dt.Columns.Add("AdminName");//

源文件: H:\NEWS_CHANEL\Backup\NEWS_CHANEL\Admin\AdminList.aspx.cs 行: 43 

堆栈跟踪: 


[DuplicateNameException: 名为“Admin_ID”的列已属于此 DataTable。]
  System.Data.DataColumnCollection.RegisterColumnName(String name, DataColumn column, DataTable table) +4841153
  System.Data.DataColumnCollection.BaseAdd(DataColumn column) +93
  System.Data.DataColumnCollection.AddAt(Int32 index, DataColumn column) +78
  System.Data.DataColumnCollection.Add(String columnName) +41
  NEWS_CHANEL.Admin.AdminList.data() in H:\NEWS_CHANEL\Backup\NEWS_CHANEL\Admin\AdminList.aspx.cs:43
  NEWS_CHANEL.Admin.AdminList.Page_Load(Object sender, EventArgs e) in H:\NEWS_CHANEL\Backup\NEWS_CHANEL\Admin\AdminList.aspx.cs:25
  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.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

后台页面:
C# code
namespace NEWS_CHANEL.Admin{    public partial class AdminList : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                data();                PageControl.ResetGridView(this.GridView1);            }        }        private void data()        {            BLL.SysAdmin admin = new BLL.SysAdmin();            SqlDataReader dr = admin.GetAdmins();            DataTable dt = SQLHelper.ConvertDrToDt(dr);            dt.Columns.Add("Admin_ID");            dt.Columns.Add("AdminName");            dt.Columns.Add("Role_ID");            DAL.PageControl.GridViewDataBind(GridView1, dt);        }    }}

SysAdmin类:
C# code
namespace BLL{    public class SysAdmin    {        //定义管理员组数值        public static readonly int Superadmin = 0;        public static readonly int Normaladmin = 1;        public SqlDataReader GetAdmins()        {            SQLHelper sql = new SQLHelper();            SqlDataReader dr = null;            //执行存储过程            try            {                sql.RunProc("Proc_Admins", out dr);            }            catch (Exception e)            {                throw e;            }            finally { }            return dr;        }    }}

PageControl类:
C# code
namespace DAL{    public class PageControl    {        private static string EmptyText = "没有相关数据!"; //数据为空时,Gridview显示提示        public PageControl()        {         }        /// <summary>        /// 空数据时刷新页面正常显示表头        /// </summary>        /// <param name="gridview">页面Gridview</param>        public static void ResetGridView(GridView gridview)        {            //数据为空时,Gridview的构造            if(gridview.Rows.Count == 1  && gridview.Rows[0].Cells[0].Text == EmptyText)            {                int countnum = gridview.Columns.Count;                gridview.Rows[0].Cells.Clear();                gridview.Rows[0].Cells.Add(new TableCell());                gridview.Rows[0].Cells[0].ColumnSpan = countnum;                gridview.Rows[0].Cells[0].Text = EmptyText;                gridview.Rows[0].Cells[0].Style.Add("text-align", "center");            }        }        /// <summary>        /// Gridview绑定数据,为空时显示表头        /// </summary>        /// <param name="gridview">页面Gridview</param>        /// <param name="datatable">数据缓存DataTable</param>        public static void GridViewDataBind(GridView gridview,DataTable datatable)        {            //数据为空时,Gridview的构造            if (datatable.Rows.Count == 0)            {                datatable = datatable.Clone();                datatable.Rows.Add(datatable.NewRow());                gridview.DataSource = datatable;                gridview.DataBind();                int countnum = gridview.Columns.Count;                gridview.Rows[0].Cells.Clear();                gridview.Rows[0].Cells.Add(new TableCell());                gridview.Rows[0].Cells[0].ColumnSpan = countnum;                gridview.Rows[0].Cells[0].Text = EmptyText;                gridview.Rows[0].Cells[0].Style.Add("text-align", "center");            }            else            {                //数据不为空,直接绑定                gridview.DataSource = datatable;                gridview.DataBind();            }            //取消行选择            gridview.SelectedIndex = -1;        }    }}
  相关解决方案