//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,就出现“未将对象引用设置到对象实例” 不知道哪里的事情。哪位大侠帮帮我
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,就出现“未将对象引用设置到对象实例” 不知道哪里的事情。哪位大侠帮帮我
解决方案 »
- 高分求如何在asp.net中将数据库中binary字段存储的rtf格式的带文本和图片的内容读取到ie中的方法,可以用控件,但不能用文件方式存储rtf
- .Net 线程 线程池的应用
- 关于CMS的前景,请大家探讨一下
- Win2003 环境下只运行asp、aspx还需要装apache吗?
- 工具箱丢失WEB控件
- 遇到语法问题,在asp.net如何使用ADO?
- 一个正式表达式的分割问题。
- 请问怎样把DataGrid保存成Excel文件?
- 求解,一个ASP.NET MVC的问题,有人已经问过了,没有解答
- //====我在把asp.net页面转为excel时,转出的内容却是登录界面,还是乱码。===
- 新人在线求助
- 怎样让datagrid 中HyperLinkColumn的DataNavigateUrlField绑定两个属性
像这个“未将对象引用”的错一般都你在cs文件里面声明了一个对象,但是在对应的aspx里面找不到