我在做一个网上书店的系统,当在首页的分类列表点击图书类别时,就回出现以下错误:
  “/WebApplication2”应用程序中的服务器错误。
--------------------------------------------------------------------------------未将对象引用设置到对象的实例。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误: 
行 28:  Project2.ProductDB productCatalogue = new Project2.ProductDB();
行 29: 
行 30:  DataList2.DataSource = productCatalogue.GetProducts(categoryId);//出错那一句
行 31: 
行 32:  DataList2.DataBind();
 源文件: e:\网上书店\webapplication2\productslist.aspx.cs    行: 30 堆栈跟踪: 
[NullReferenceException: 未将对象引用设置到对象的实例。]
   WebApplication2.productslist.Page_Load(Object sender, EventArgs e) in e:\网上书店\webapplication2\productslist.aspx.cs:30
   System.Web.UI.Control.OnLoad(EventArgs e) +67
   System.Web.UI.Control.LoadRecursive() +35
   System.Web.UI.Page.ProcessRequestMain() +750 
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.2300; ASP.NET 版本:1.1.4322.23下面的代码是图书列表页(ProductList.aspx.cs)的后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace WebApplication2
{
/// <summary>
/// productslist 的摘要说明。
/// </summary>
public class productslist : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlTableCell TD1;
protected System.Web.UI.HtmlControls.HtmlTable TABLE1;
protected System.Web.UI.WebControls.DataList DataList2;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
int categoryId=Int32.Parse(Request.Params["CategoryID"]);
            
Project2.ProductDB productCatalogue = new Project2.ProductDB();//系统显示有问题 DataList2.DataSource = productCatalogue.GetProducts(categoryId); DataList2.DataBind(); } #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}下面的是数据层的代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;namespace Project2
{
/// <summary>
/// 商品详细信息类
/// </summary>
public class ProductDetails
{
  //定义商品型号
public String ModeNumber;
      //定义商品名称
public String ModeName;
      //定义商品的图片位置
public String ProductImage;
  //定义商品价格
public decimal UnitCost;
  //定义商品说明
public String Description;
} /// <summary>
/// ProductDB 的摘要说明。
/// </summary>
public class ProductDB
{
public SqlDataReader GetProductCateGories()
{
    SqlConnection myConnection= new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand=new SqlCommand("ProductCategoryList",myConnection);
myCommand.CommandType=CommandType.StoredProcedure;
myConnection.Open();
SqlDataReader result=myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}                //调用的函数
public SqlDataReader GetProducts(int categoryID)
{
    SqlConnection myConnection= new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlCommand myCommand=new SqlCommand("ProductsByCategory",myConnection);
myCommand.CommandType=CommandType.StoredProcedure; SqlParameter parameterCategoryID = new SqlParameter("@CategoryID",SqlDbType.Int,4);
parameterCategoryID.Value=categoryID;
myCommand.Parameters.Add(parameterCategoryID); myConnection.Open();
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
}
}请大家看看有什么问题,谢谢了!

解决方案 »

  1.   

    有可能你的sql语句有问题~lz瞄瞄
      

  2.   

    Project2.ProductDB productCatalogue = new Project2.ProductDB();//系统显示有问题什么意思?
      

  3.   

    你叶面上到底有没有一个叫DataList2的东西?把DataList2的声明删掉,重新添加这个控件。
      

  4.   

    可以追踪一个执行过程,看看到底在哪一步出了错,看样子应该是在你的返回Reader时出了问题
      

  5.   

    Project2.ProductDB productCatalogue = new Project2.ProductDB();//系统显示有问题什么意思?
    这个是出错的时候,有问题的那个句子
      

  6.   

    你叶面上到底有没有一个叫DataList2的东西?把DataList2的声明删掉,重新添加这个控件有,是一个用户控件