<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewDynamic.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="labelStatus" runat="server" Style="z-index: 100; left: 46px; position: absolute;
top: 49px" Text="labelStatus"></asp:Label>
<asp:TreeView ID="TreeView1" runat="server" MaxDataBindDepth="2" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
Style="z-index: 102; left: 42px; position: absolute; top: 100px">
<Nodes>
<asp:TreeNode PopulateOnDemand="True" Text="产品列表" Value="产品列表"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "CATEGORYID", "CATEGORYNAME" FROM "CATEGORIES"'>
</asp:SqlDataSource>
</form>
</body>
</html>using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TreeView1_TreeNodePopulate(
object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
switch (e.Node.Depth)
{
case 0:
PopulateCategories(e.Node);
break;
case 1:
PopulateProducts(e.Node);
break;
}
}
} void PopulateCategories(TreeNode node)
{
OracleCommand sqlQuery = new OracleCommand(
"Select CategoryName, CategoryID From Categories");
DataSet resultSet;
resultSet = RunQuery(sqlQuery);
if (resultSet.Tables.Count > 0)
{
foreach (DataRow row in resultSet.Tables[0].Rows)
{
TreeNode NewNode = new
TreeNode(row["CategoryName"].ToString(),
row["CategoryID"].ToString());
NewNode.PopulateOnDemand = true;
NewNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(NewNode);
}
}
} void PopulateProducts(TreeNode node)
{
OracleCommand sqlQuery = new OracleCommand();
sqlQuery.CommandText = "Select ProductName From Products " +
" Where CategoryID = @categoryid";
sqlQuery.Parameters.Add("@categoryid", SqlDbType.Int).Value =
node.Value;
DataSet ResultSet = RunQuery(sqlQuery);
if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode NewNode = new
TreeNode(row["ProductName"].ToString());
NewNode.PopulateOnDemand = false;
NewNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(NewNode);
}
}
}
private DataSet RunQuery(OracleCommand sqlQuery)
{
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
OracleConnection DBConnection =
new OracleConnection(connectionString);
OracleDataAdapter dbAdapter = new OracleDataAdapter();
dbAdapter.SelectCommand = sqlQuery;
sqlQuery.Connection = DBConnection;
DataSet resultsDataSet = new DataSet();
try
{
dbAdapter.Fill(resultsDataSet);
}
catch
{
labelStatus.Text = "Unable to connect to SQL Server.";
}
return resultsDataSet;
}
}
我运行的时候,string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
这段代码报错,提示未将对象引用设置到对象的实例。请问这是什么原因?我这些代码都是从MSDN里面复制出来的。只是把SQL改成oracle数据库,谁能帮我看看这代码,哪错了,谢谢了。
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="labelStatus" runat="server" Style="z-index: 100; left: 46px; position: absolute;
top: 49px" Text="labelStatus"></asp:Label>
<asp:TreeView ID="TreeView1" runat="server" MaxDataBindDepth="2" OnTreeNodePopulate="TreeView1_TreeNodePopulate"
Style="z-index: 102; left: 42px; position: absolute; top: 100px">
<Nodes>
<asp:TreeNode PopulateOnDemand="True" Text="产品列表" Value="产品列表"></asp:TreeNode>
</Nodes>
</asp:TreeView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand='SELECT "CATEGORYID", "CATEGORYNAME" FROM "CATEGORIES"'>
</asp:SqlDataSource>
</form>
</body>
</html>using System;
using System.Data;
using System.Data.OracleClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TreeView1_TreeNodePopulate(
object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
switch (e.Node.Depth)
{
case 0:
PopulateCategories(e.Node);
break;
case 1:
PopulateProducts(e.Node);
break;
}
}
} void PopulateCategories(TreeNode node)
{
OracleCommand sqlQuery = new OracleCommand(
"Select CategoryName, CategoryID From Categories");
DataSet resultSet;
resultSet = RunQuery(sqlQuery);
if (resultSet.Tables.Count > 0)
{
foreach (DataRow row in resultSet.Tables[0].Rows)
{
TreeNode NewNode = new
TreeNode(row["CategoryName"].ToString(),
row["CategoryID"].ToString());
NewNode.PopulateOnDemand = true;
NewNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(NewNode);
}
}
} void PopulateProducts(TreeNode node)
{
OracleCommand sqlQuery = new OracleCommand();
sqlQuery.CommandText = "Select ProductName From Products " +
" Where CategoryID = @categoryid";
sqlQuery.Parameters.Add("@categoryid", SqlDbType.Int).Value =
node.Value;
DataSet ResultSet = RunQuery(sqlQuery);
if (ResultSet.Tables.Count > 0)
{
foreach (DataRow row in ResultSet.Tables[0].Rows)
{
TreeNode NewNode = new
TreeNode(row["ProductName"].ToString());
NewNode.PopulateOnDemand = false;
NewNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(NewNode);
}
}
}
private DataSet RunQuery(OracleCommand sqlQuery)
{
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
OracleConnection DBConnection =
new OracleConnection(connectionString);
OracleDataAdapter dbAdapter = new OracleDataAdapter();
dbAdapter.SelectCommand = sqlQuery;
sqlQuery.Connection = DBConnection;
DataSet resultsDataSet = new DataSet();
try
{
dbAdapter.Fill(resultsDataSet);
}
catch
{
labelStatus.Text = "Unable to connect to SQL Server.";
}
return resultsDataSet;
}
}
我运行的时候,string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
这段代码报错,提示未将对象引用设置到对象的实例。请问这是什么原因?我这些代码都是从MSDN里面复制出来的。只是把SQL改成oracle数据库,谁能帮我看看这代码,哪错了,谢谢了。
你应该先写一个连接的sql语句看看,最好不要一开始就执行这么复杂的语句