我是 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 可以(并且检查根不是 0 而不是 null),但最后,如何在网页中显示?