4

朝 (少なくとも 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 に変換して、メニューの DataSource として使用するために変換される xmlDataSource で使用します。

これらのフォーラムから多くのコードを取得したことを認めなければなりませんが、それは機能するはずです。変換を除いて、最上位のメニュー項目を示すために 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 を設定する方法、または失敗した場合、変換を許可するように設定する方法 (たとえば) NULL の代わりに 0。

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>

これまでご愛顧いただき、誠にありがとうございました!

よろしく

ポール

4

2 に答える 2

2

cntIuvo.MenuNavs の ParentID は null 可能ですか?

myDR["ParentID"] = rec.ParentID ?? Convert.DBNull; // Replace null value to DBNull
于 2011-09-11T07:32:25.353 に答える
0

こんな感じかも?myDR["ParentID"] = rec.ParentID == null ? DBNull.Value : rec.ParentID;

于 2011-09-11T07:32:08.357 に答える