//orders.aspx文件
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 Northwind
{
/// <summary>
/// Orders 的摘要说明。
/// </summary>
public class Orders : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DG_Orders;
    protected DBAccess DB=new DBAccess(); protected void DG_Orders_Bind()
{
string SelectStr="Select * from Orders";
DB.BindDataGrid(SelectStr,DG_Orders);
}

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
DG_Orders_Bind();

} #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
}
}
下面是DBAccess
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;namespace Northwind
{
/// <summary>
/// DBAccess 的摘要说明。主要是数据库草错的方法,附带一些常用的小函数
/// </summary>
public class DBAccess
{
#region 变量声明
protected SqlConnection Conn;
protected DataSet DS;
protected SqlDataAdapter SDA;
protected SqlCommand SC;
protected SqlDataReader SDR;
protected HttpResponse Response;
protected HttpSessionState Session;
#endregion public DBAccess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void Open()
{
try 
{
string ConnStr=System.Configuration.ConfigurationSettings.AppSettings["conn"].ToString().Trim();
Conn=new SqlConnection(ConnStr);
Conn.Open(); }
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void Close()
{
try

if(Conn.State==ConnectionState.Open)
Conn.Close();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
}
public void Dispose()
{
if(DS!=null)
DS.Dispose();
if(SDA!=null)
SDA.Dispose();
if(SDR!=null)
{
if(!SDR.IsClosed)
SDR.Close();
}
}
public void Fill(string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
} public void Fill(string TableName,string strSql)
{
try
{
Open();
SDA=new SqlDataAdapter(strSql,Conn);
DS=new DataSet();
SDA.Fill(DS,TableName);
}
catch(Exception e)
{
WriteMessage(e.Message.ToString().Trim(),true,true);
}
finally
{
Close();
Dispose();
}
}
public void BindDataGrid(string strSql,DataGrid DG)
{
try
{
Open();
Fill(strSql);
DG.DataSource=DS.Tables[0].DefaultView;
if(DG.CurrentPageIndex>DG.PageCount-1)
{
if(DG.PageCount>0)
DG.CurrentPageIndex=DG.PageCount-1;
else
DG.CurrentPageIndex=0;
}
DG.DataBind();
}
catch(Exception e)
{
WriteMessage(e.Message.ToString(),true,true);
}
finally
{
Close();
Dispose();
}
}
}
我只要一运行orders.aspx,就出现“未将对象引用设置到对象实例” 不知道哪里的事情。哪位大侠帮帮我