【问题标题】:Error while binding Treeview Control with Database using NHibernate使用 NHibernate 将 Treeview 控件与数据库绑定时出错
【发布时间】:2009-12-31 04:13:02
【问题描述】:

这是我的应用程序的代码解释和映射文件

private void PopulateNodes(IList<Folder> Values, TreeNodeCollection nodes)
        {

            foreach (Folder r in Values)
            {

                TreeNode tn = new TreeNode();
                tn.Text = r.folderName.ToString();
                tn.Value = r.folderID.ToString();
                nodes.Add(tn);
                bool val = _documentManagerModule.GetNodeCount((int)r.folderID);
                tn.PopulateOnDemand = val;



            }


        }

另外一种方法如下

private ISessionFactory m_SessionFactory = null;

public bool GetNodeCount(int parentfolderID)
        {

          ISession session = m_SessionFactory.OpenSession();
          int count = (int)session.CreateQuery("select count(f.parentID) from Folder f  where f.parentID = ?").SetParameter(0, parentfolderID.ToString()).UniqueResult();

            try
            {
                if (count > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to get ChildFolders ", ex);
            }   
}

而我的映射文件如下

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  namespace ="Cuyahoga.Modules.DocumentManager"
  assembly ="Cuyahoga.Modules.DocumentManager">
  <!-- Mappings for class 'Folder' -->

  <class name="Folder" table="cm_folders" lazy="false">

    <!-- Identity mapping -->
    <id name="folderID">
      <column name="folder_ID"  />
      <generator class="native" />
    </id>

    <!-- Simple mappings -->

    <property name="folderName" column ="folder_name" />
    <property name="parentID" column ="parent_ID" />



    <!-- one-to-many mapping: Document-->

    <bag name="Documents" table ="cm_documents" cascade="save-update" lazy="false">
      <key column="folder_ID" />
      <one-to-many class="Document" />
    </bag>
     <!--Reflexive associations-->
    <many-to-one name="ParentFolder" class ="Folder" column ="parent_ID" cascade ="none"/>

    <bag name="ChildFolder" table ="cm_folders" cascade="save-update" inverse ="true" lazy="false">
      <key column="parent_ID" />
      <one-to-many class="Folder" />
    </bag>


  </class>
</hibernate-mapping>

我得到的错误是

对象引用未设置为对象的实例。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.NullReferenceException:对象引用未设置为对象的实例。

来源错误: 在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。

堆栈跟踪: [NullReferenceException:对象引用未设置为对象的实例。] Cuyahoga.Modules.DocumentManager.DocumentManagerModule.GetNodeCount(Int32 parentfolderID) +84 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateNodes(IList`1 值,TreeNodeCollection 节点)+288 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.PopulateRootLevel() +94 Cuyahoga.Modules.DocumentManager.Web.DocumentManager.Page_Load(对象发送者,EventArgs e)+121 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,对象 o,对象 t,EventArgs e)+14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发送者,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.Control.LoadRecursive() +141 System.Web.UI.Control.LoadRecursive() +141 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

请帮我找出这个程序中的错误。实际上我正在尝试使用 NHibernate 将树视图控件与数据库绑定。作为实现的一部分,我试图检查当前父节点是否有一些子节点.如果它有子节点,则布尔值设置为 true,并且该节点的 PopulateOnDemand 属性设置为 true。

【问题讨论】:

  • GetNodeCount 返回一个布尔值是相当具有欺骗性的。我建议您将函数重命名为 HasNodes 或从函数返回实际节点数。

标签: c# nhibernate exception treeview controls


【解决方案1】:

从外观上看,m_SessionFactory 似乎没有在GetNodeCount 方法中初始化。这就是我要开始寻找的地方。

此外,您可以将整个 if/else 块替换为 return (count &gt; 0)。虽然,如果方法名为GetNodeCount,它确实应该返回一个count

【讨论】:

  • m_SessionFactory 已在 GetNodeCount 方法之外初始化。但错误仍然相同。我在上面的代码中更新了它
  • null 未初始化。你得到一个NullReferenceException,因为m_SessionFactory实际上是null。如果它实际上在某处被设置为ISessionFactory 的实例,则它不会出现在发布的代码中。
猜你喜欢
  • 2021-09-20
  • 2013-05-18
  • 1970-01-01
  • 2010-10-16
  • 1970-01-01
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多