Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将NULL插入数据表_C#_Asp.net_Linq To Entities_Datatable_Datarelation - Fatal编程技术网

C# 将NULL插入数据表

C# 将NULL插入数据表,c#,asp.net,linq-to-entities,datatable,datarelation,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 de

早晨(至少在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实体来获取这些数据。然后我填充一个DataTable,然后填充一个DataSet,然后创建一个DataRelationship,然后将其转换为XML以在xmlDataSource中使用,在xmlDataSource中转换后用作菜单的数据源

我必须承认,我从这些论坛上获取了很多代码,应该可以使用。除了转换需要ParentID中的NULL值来指示顶级菜单项之外,我不能将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"]));
        }
<?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>
我的问题是:如何将NULL插入到数据表中,或者,如果失败,如何让LINQ to实体填充数据表/数据集,或者如果失败,如何设置转换以允许(比如)0而不是NULL

Transform.xslt如下所示:

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

?Sel=
非常感谢您迄今为止的关注

问候

保罗可能是这样的?
myDR[“ParentID”]=rec.ParentID==null?值:rec.ParentID

cntIuvo.menunnas的ParentID是否可以为空

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

您好mishou,谢谢您的帮助,但是如上所述,Null和DBNull都会导致问题。干杯,保罗/你试过逐步调试了吗?菜单记录中的eaxctly rec.ParentID是什么?空还是什么?异常的类型名称是什么?我测试了你的一段代码,用DBNull.Value填充了所有的字段,效果很好!谢谢Fred,但是表中也不接受DBNull。我可能不得不求助于“原始”SQL来直接将数据获取到数据表中。任何转换似乎都会在涉及Null时停止。我可以;当我在关系构建中遇到循环引用错误时,请不要用Itegers填充原始表。。。但是谢谢你的帮助。PaulIs“ParentID”列是否可以为null(DataColumn.AlllowDBNull为true)?