访问SQL Server时出现的“未将对象引用设置到对象的实例”错误。
本人最近在VS2003+SQL Server2000环境下开发一个Web系统,下操作系统环境是Windows XP(SP2),在开发过程中遇到过如下的怪问题。开始的时候一切很正常,就突然有一天出现“未将对象引用设置到对象的实例”错误(英文错误为Object reference not set to an instance of an object)。
连接SQL Server测试完整代码如下(文件名为):
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<script runat="server">
void Page_Load(object sender, System.EventArgs e)
{SqlConnection sqlConnection1=new SqlConnection("Data Source=GISCIS;database=northwind;uid=sa;pwd=sa");
SqlCommand sqlCommand1=new SqlCommand ();
sqlCommand1.CommandText = "select count(*) from Products";
sqlCommand1.Connection = sqlConnection1;
sqlConnection1.Open();
Int32 rows=(Int32)sqlCommand1.ExecuteScalar();
Response.Write("当前记录共有:" + rows+ "行");
}
</script>
</head>
<body>
</body>
</html>
运行后完整错误如下:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
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.NullReferenceException: Object reference not set to an instance of
an object.
Source Error:
Line 9: sqlCommand1.CommandText = "select count(*) from Products";
Line 10: sqlCommand1.Connection = sqlConnection1;
Line 11: sqlConnection1.Open();
Line 12: Int32 rows=(Int32)sqlCommand1.ExecuteScalar();
Line 13: Response.Write("当前记录共有:" + rows+ "行");
Source File: c:\inetpub\wwwroot\CmdSample.aspx Line: 11
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
LoadLibraryW(UInt16* ) +0
System.EnterpriseServices.Thunk.Security.Init() +76
System.EnterpriseServices.Thunk.Security.SuspendImpersonation() +81
System.EnterpriseServices.Platform.Initialize() +503
System.EnterpriseServices.ResourcePool..ctor(TransactionEndDelegate cb) +11
System.Data.SqlClient.ConnectionPool..ctor(DefaultPoolControl ctrl) +797
System.Data.SqlClient.PoolManager.FindOrCreatePool(DefaultPoolControl ctrl) +170
System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConnectionString
options, Boolean& isInTransaction) +358
System.Data.SqlClient.SqlConnection.Open() +384
ASP.CmdSample_aspx.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\CmdSample.aspx:11
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573
line11行红色提示。
我当时忙其他的事也没时间管它,也没找具体错误的原因,但过几天自己好了,运行一切正常。接着开发,几天后又出现“未将对象引用设置到对象的实例”的奇怪问题,于是上网查资料。yuanchuan2001 () 在程序员大本营发了《让人崩溃的未将对象引用设置到对象的实例~~~》贴;Lavy发表了《重复定义造成未将对象引用设置到对象的实例错误》贴;还有的贴说是如下问题:(1)所设置的变量为空值或没有取到值,一般出现在传递参数的时候出现这个问题,也会在使用datagrid或gridview或datalist等数据控件时出现(2)控件名称与codebehind里面的没有对应(3)未用new初始化对象(4)在程序中所引用的控件不存在。在《让人崩溃的未将对象引用设置到对象的实例~~~》贴中likesunny(阳光) 的回复是“我也有碰到, 是程序里面有相同页面名字,或者其它类似的冲突,有时可以认到有时无法运行!你不妨可以从这个方面查查”。我于是把IIS里所有的虚拟目录删掉,仅留SQL Server2000连接测试用的一个例子CmdSample.aspx,果然可以,用VS2003新建项目写连接SQL Server的语句运行一切正常。但好景不常,第二天又出现“未将对象引用设置到对象的实例”的错误。于是开始怀疑是代码有问题,但把CmdSample.aspx拷到其他相同环境的电脑上运行可以正常运行,可以肯定代码没问题。我的电脑上不仅安装了VS2003,而且安装了VS2005,于是在IIS里选择Microsoft .NET Framework2.0(原来选择的是Microsoft .NET Framework1.1),CmdSample.aspx可以正常运转(可以确定SQL Server2000没有问题),一换到就出现“未将对象引用设置到对象的实例”错误。当我把SQLServer服务器停止掉,理论上应该出现“SQLServer 不存在或者连接不上”的错误,奇怪的是出现的错误仍然是“未将对象引用设置到对象的实例”的错误, 也就是说出现的错误不是因为SQL SERVER2000。但不放心,还是把SQL SERVER2000卸载掉,卸载谢之后,在没有安装SQL SERVER2000的情况下运行CmdSample.aspx,仍然出现“未将对象引用设置到对象的实例”的错误。再把SQL SERVER2000安装上,运行CmdSample.aspx,错误仍然如此。于是怀疑Microsoft .NET Framework1.1和Microsoft .NET Framework2.0有冲突,就把VS2005所有相关的内容都卸载掉,仍然出现“未将对象引用设置到对象的实例”的错误。郁闷之下,把VS2003的相关内容都卸载掉,重新安装IIS,安装Microsoft .NET Framework1.1运行环境,但问题仍然存在。我每次安装卸载东西都重新启动了系统。
目前问题还没解决,准备重新安装系统了。
哪位大哥遇到此问题,欢迎一起讨论!
浙公网安备 33010602011771号