私はasp.netが初めてで、asp.netを学習するためにデータベースからツリービューを構築しようとしています.インターネットからコードをダウンロードして実行しようとしています. しかし、いくつかのエラーがあり、何が問題なのかよくわかりません
ここにテーブルがあります
CREATE TABLE [dbo].[node](
[id] [int] NOT NULL,
[title] [varchar](255) NULL,
[parent_id] [int] NULL,
[oid] [int] NULL,
[display] [tinyint] NULL,
CONSTRAINT [PK_note] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
私がいくつかの変更を加えたコード
public partial class index : System.Web.UI.Page
{
private static TreeView TreeView1 = new TreeView();
protected void Page_Load(object sender, EventArgs e)
{
buildTree();
}
private void buildTree()
{
SqlConnection dbCon = new SqlConnection(ConfigurationManager.ConnectionStrings["earchConnectionString"].ConnectionString);
dbCon.Open();
string sql = "Select * from [node]";
SqlDataAdapter adapter = new SqlDataAdapter(sql, dbCon);
DataSet ds = new DataSet();
adapter.Fill(ds);
dbCon.Close();
ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parent_id"]);
foreach (DataRow dbRow in ds.Tables[0].Rows)
{
if (dbRow["parent_id"]=="0")
{
TreeNode newNode = CreateNode(dbRow["title"].ToString(), dbRow["id"].ToString(), true);
TreeView1.Nodes.Add(newNode);
PopulateSubTree(dbRow, newNode);
}
}
}
private void PopulateSubTree(DataRow dbRow, TreeNode node)
{
foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
{
TreeNode childNode = CreateNode(childRow["title"].ToString(), childRow["id"].ToString(), true);
node.ChildNodes.Add(childNode);
PopulateSubTree(childRow, childNode);
}
}
//
private TreeNode CreateNode(string text, string id, bool expanded)
{
TreeNode node = new TreeNode(); ;
node.Text = text;
node.Value = id;
node.Expanded = true;
return node;
}
}
エラー:
This constraint cannot be enabled as not all values have corresponding parent values.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.
Source Error:
Line 32: dbCon.Close();
Line 33:
Line 34: ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["parent_id"]);
Line 35: foreach (DataRow dbRow in ds.Tables[0].Rows)
Line 36: {
Source File: c:\inetpub\web1\index.aspx.cs Line: 34
Stack Trace:
[ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.]
System.Data.ConstraintCollection.AddForeignKeyConstraint(ForeignKeyConstraint constraint) +1932628
System.Data.ConstraintCollection.Add(Constraint constraint, Boolean addUniqueWhenAddingForeign) +468
System.Data.DataSetRelationCollection.AddCore(DataRelation relation) +947
System.Data.DataRelationCollection.Add(DataRelation relation) +169
System.Data.DataRelationCollection.Add(String name, DataColumn parentColumn, DataColumn childColumn) +47
index.buildTree() in c:\inetpub\web1\index.aspx.cs:34
index.Page_Load(Object sender, EventArgs e) in c:\inetpub\web1\index.aspx.cs:17
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
誰でも問題を定義できますか? 私はparent_idをnullに変更しましたが、これで問題ありません(そしてnullではなく0ではなくルートをチェックします)が、最後に、Webページに表示する方法は?