【问题标题】:Column '' does not belong to table " even though it is present in the table and the query列''不属于表“,即使它存在于表和查询中
【发布时间】:2011-11-18 11:06:15
【问题描述】:

错误信息是:

引发了“System.Web.HttpUnhandledException”类型的异常。 友好消息:System.ArgumentException:列'testfile'确实 不属于表。在 System.Data.DataRow.GetDataColumn(字符串 columnName) 在 System.Data.DataRow.get_Item(String columnName) 在 Registration_test.FillImage() 中 c:\inetpub\wwwroot\testWeb\Events\Registration.aspx.cs:第 49 行 Registration_test.Page_PreInit(Object sender, EventArgs e) in c:\inetpub\wwwroot\testWeb\test\Registration.aspx.cs:第 39 行 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e) 在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者, EventArgs e) 在 System.Web.UI.Page.OnPreInit(EventArgs e) 在 System.Web.UI.Page.PerformPreInit() 在 System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) 目标站点:Boolean HandleError(System.Exception) 源:System.Web 错误堆栈跟踪是:在 System.Web.UI.Page.HandleError(Exception e) 在 System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) 在 System.Web.UI.Page.ProcessRequest(布尔 includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) 在 System.Web.UI.Page.ProcessRequest() 在 System.Web.UI.Page.ProcessRequest(HttpContext 上下文)在 ASP.events_registration_aspx.ProcessRequest(HttpContext context) 在 c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET 文件\testWeb\761ce6e4\d915dd85\App_Web_posk8hm6.6.cs:第 0 行 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, Boolean & completedSynchronously)

当站点托管在 IIS 中时,我收到了上述错误作为电子邮件,但是当我在本地甚至在线检查代码时我没有收到它。错误表示即使查询返回 testfile 列也不属于 datable列。

任何人都可以帮助解决可能的原因..

///////////////////这是我的代码////////////////// //////////////////////////

public partial class Registration_Registration : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {

       FillImage();
    }
    protected void FillImage()
    {

       string Query = "select t.header_image_file as  'HeaderImage',t.image,c.testfile from registrars c left join trade t on c.tradeid =  t.tradeid and c.mainitemid = t.mainitemid ";


       DataTable result = GetDataTable(Query);

       if (result != null && result .Rows.Count > 0)
       {
          if (result .Rows[0]["testfile"] != null)
          {
             this.Theme = result .Rows[0]["testfile"].ToString().Trim();
             this.MasterPageFile = result .Rows[0]["testfile"].ToString().ToLower().Trim() + ".master";
          }
       }
   }
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
    }
 }}

【问题讨论】:

  • 你想在你的代码中做什么?Registration.aspx.cs:line 49 只需检查一下,请更新你的答案
  • 我不赞成:您从系统中复制/粘贴一条错误消息,甚至没有阅读它:“列'testfile'不属于表”->您没有在您的生产数据库!并在标题中更加明确。
  • remi bourgarel 希望你喜欢我的新标题。
  • 能不能给使用GetDataTable的内容?
  • 您找到解决方案了吗?我也有类似的情况。直到昨天这些错误开始出现时,一切都运行良好。据我所知,没有做出任何改变。而且我知道这是非常随机的,因为许多用户能够访问该页面,而对于某些页面错误。我一开始以为只是在高负载期间,但即使在低负载时间我也能看到错误。如果您发现任何有用的信息,请告诉我。谢谢!

标签: asp.net sql ado.net


【解决方案1】:

好吧,乍一看,我们发现错误与不在表中的列有关。

要解决此问题,我建议您首先在管理工作室中运行查询,检查返回的列名称与您尝试在 ASP.Net 页面中访问的列名称。 如果列名都是正确的备份数据然后删除并重新创建表和访问该表的存储过程,希望这将解决问题。

在开发过程中意外更新数据库中的列等是相当容易的,而不是将更改推送到生产数据库。

如果这不能解决问题,如果您发布表结构、查询和访问查询的代码,您将能够获得更好的帮助,以便我们更有效地诊断问题。

更新: 这些是您在查询中选择的列

strt.header_image_file as  'HeaderImage',
t.image,
c.themefile  

但在您的代码中,您正试图访问一个名为 testfile 的列

【讨论】:

  • 我已经在管理工作室中对数据库运行了查询,并且该列存在于表中,这个错误我并不总是得到这个错误是间歇性发生的。
  • @PakaujiPakau 我用我认为导致问题的原因更新了我的答案。让我知道它是否不正确。谢谢
  • 实际上这是编辑时的拼写错误,我已经更新了该列是 testfile 的帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多