加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在DataTable中插入NULL

发布时间:2020-12-15 17:19:57 所属栏目:百科 来源:网络整理
导读:早上(至少在RSA), 我正在尝试创建一个数据驱动的菜单,使用来自自引用表的数据来创建(2级)层次结构.示例数据是: MenuID ParentID Text Url CSS1 Null Top topCSS2 Null Second secCSS3 1 abc z.aspx abcCSS4 1 def y.aspx abcCSS5 2 ghi x.aspx defCSS 我正
早上(至少在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来获取这些数据.然后我填充DataTable,然后填充DataSet,然后创建DataRelation,然后将其转换为XML以在xmlDataSource中使用,在xmlDataSource中将其转换为Menu的DataSource.

我必须承认我从这些论坛中获取了很多代码,它应该可行.除了转换需要ParentID中的NULL值来指示顶级菜单项,但我无法在DataTable中插入NULL.代码如下:

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"]));
        }

我的问题是:如何在DataTable中插入NULL,或者,如果失败,如何让LINQ to Entities填充DataTable / DataSet,或者如何设置转换以允许(比如说) a 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读