c# - 将 NULL 插入 DataTable

标签 c# asp.net linq-to-entities datatable datarelation

早上(至少在 RSA 中),

我正在尝试创建一个数据驱动的菜单,使用自引用表中的数据创建(2 级)层次结构。示例数据是:

MenuID  ParentID  Text      Url      CSS
1       Null      Top                topCSS
2       Null      Second             secCSS
3       1         abc       z.aspx   abcCSS
4       1         def       y.aspx   abcCSS
5       2         ghi       x.aspx   defCSS

我正在使用 LINQ to Entities 来获取这些数据。然后我填充一个数据表,然后填充一个数据集,然后创建一个数据关系,然后将其转换为 XML 以在 xmlDataSource 中使用,在那里它被转换为用作菜单的数据源。

我必须承认我从这些论坛中获取了大量代码,它们应该 可以工作。除了转换需要 ParentID 中的 NULL 值来指示顶级菜单项外,但我无法将 NULL 插入到 DataTable 中。代码如下:

        using (var cntIuvo = new iuvocexi_dbEnts())
        {
            var b = (from a in cntIuvo.MenuNavs select a);
            DataTable myTB = new DataTable();
            myTB.Columns.Add("MenuID");
            myTB.Columns.Add("ParentID");
            myTB.Columns.Add("url");
            myTB.Columns.Add("CSS");
            DataRow myDR;

            foreach (var rec in b)
            {
                myDR = myTB.NewRow();
                myDR["MenuID"] = rec.MenuID;
                myDR["ParentID"] = rec.ParentID;  // error is generated here
                myDR["url"] = rec.url;
                myDR["CSS"] = rec.CSS;
                myTB.Rows.Add(myDR);
            }

            DataSet ds = new DataSet(); 
            ds.Tables.Add(myTB);
            ds.DataSetName = "Menus";
            ds.Tables[0].TableName = "Menu";
            DataRelation relation = new DataRelation("ParentChild", ds.Tables["Menu"].Columns["MenuID"], ds.Tables["Menu"].Columns["ParentID"], true);
            relation.Nested = true;
            ds.Relations.Add(relation);
            xmlDataSource1.Data = ds.GetXml();
            if (Request.Params["Sel"] != null)
                Page.Controls.Add(new System.Web.UI.LiteralControl("You selected " +
                  Request.Params["Sel"]));
        }

我的问题是:如何将 NULL 插入到 DataTable 中,或者,如果失败,我如何让 LINQ to Entities 填充 DataTable/DataSet,或者,如果失败,我如何设置转换以允许(比如说)一个 0 而不是 NULL。

Transform.xslt 如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <!-- Replace root node name Menus with MenuItems
   and call MenuListing for its children-->
  <xsl:template match="/Menus">
    <MenuItems>
      <xsl:call-template name= "MenuListing" />
    </MenuItems>
  </xsl:template>

  <!-- Allow for recursive child nodeprocessing -->
  <xsl:template name="MenuListing">
    <xsl:apply-templates select ="Menu" />
  </xsl:template>

  <xsl:template match="Menu">
    <MenuItem>
      <!-- Convert Menu child elements to MenuItem attributes -->
      <xsl:attribute name="Text">
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <xsl:attribute name="ToolTip">
        <xsl:value-of select="Text"/>
      </xsl:attribute>
      <xsl:attribute name="NavigateUrl">
        <xsl:text>?Sel=</xsl:text>
        <xsl:value-of select = "url"/>
      </xsl:attribute>

      <!-- Recursively call MenuListing forchild menu nodes -->
       <xsl:if test="count(Menu) >0">
         <xsl:call-template name="MenuListing" />
       </xsl:if>
    </MenuItem>
  </xsl:template>
</xsl:stylesheet>

非常感谢您到目前为止的关注!

问候

保罗

最佳答案

cntIuvo.MenuNavs 的 ParentID 是否可以为空?

myDR["ParentID"] = rec.ParentID ?? Convert.DBNull; // Replace null value to DBNull

关于c# - 将 NULL 插入 DataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7376888/

相关文章:

asp.net - 4.5 中的异步 : Cancellation and Timeout

javascript - 仅当光标位于输入字段时禁用回车键回发

mysql - Entity Framework 添加方法不可用

c# - 如何根据程序的性质显示消息

c# - 从矢量方向获得适当的旋转

c# - 在 ASP.NET 的上下文中,为什么 Task.Run(...).Result 在调用异步方法时不会死锁?

c# - 带有字体大小的 OpenXML 奇怪行为

c# - 嵌套 PredicateBuilder 谓词 : 'The parameter ' f' was not bound in the specified LINQ to Entities query expression'

linq - 使用 Linq 在 Order by 中进行数学计算

java - 尝试在 Xamarin 中绑定(bind) Glide