未将对象引用设置到对象的实例问题

spidershark 2012-04-24 10:03:20
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 100: }
行 101: List<EducationClass> List_EducationClass = new List<EducationClass>();
行 102: while (rec.Read())(这里出错了)
行 103: {
行 104: EducationClass EducationClass = new EducationClass();


运行时没有错误,但是只要点击菜单超过七次马上就出现这个问题了,不明白原因,是缓存的问题还是代码的问题?

代码:

/// <summary>
/// 查看全部分类
/// </summary>
/// <param name="nClassID"></param>
/// <returns></returns>
public List<EducationClass> Get_EducationClass(int nParentID)
{
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
SqlParameter[] ParamList ={
sqlHelper.CreateInParam("@ParentID",SqlDbType.Int,4,nParentID)
};
SqlDataReader rec = null;
try
{
sqlHelper.RunProc("Get_EducationClass", ParamList, out rec);
}
catch (Exception ex)
{
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
List<EducationClass> List_EducationClass = new List<EducationClass>();
while (rec.Read())
{
EducationClass EducationClass = new EducationClass();
EducationClass.ClassID = Int32.Parse(rec["ClassID"].ToString());
EducationClass.ClassName = rec["ClassName"].ToString();
EducationClass.ParentID = Int32.Parse(rec["ParentID"].ToString());
EducationClass.ParentOrder = Int32.Parse(rec["ParentOrder"].ToString());
List_EducationClass.Add(EducationClass);
EducationClass = null;
}
return List_EducationClass;
}
...全文
247 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ohkuy 2012-04-24
  • 打赏
  • 举报
回复

你贴的RunProc方法不对,没法看
spidershark 2012-04-24
  • 打赏
  • 举报
回复
调了好几遍了,都调不出来,郁闷
我现在怀疑是这种写法有问题!
/// <summary>
/// 查看全部分类
/// </summary>
/// <param name="nClassID"></param>
/// <returns></returns>
public List<EducationClass> Get_EducationClass(int nParentID)
{
SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
SqlParameter[] ParamList ={
sqlHelper.CreateInParam("@ParentID",SqlDbType.Int,4,nParentID)
};
SqlDataReader rec = null;
try
{
sqlHelper.RunProc("Get_EducationClass", ParamList, out rec);
}
catch (Exception ex)
{
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
List<EducationClass> List_EducationClass = new List<EducationClass>();
while (rec.Read())
{
EducationClass EducationClass = new EducationClass();
EducationClass.ClassID = Int32.Parse(rec["ClassID"].ToString());
EducationClass.ClassName = rec["ClassName"].ToString();
EducationClass.ParentID = Int32.Parse(rec["ParentID"].ToString());
EducationClass.ParentOrder = Int32.Parse(rec["ParentOrder"].ToString());
List_EducationClass.Add(EducationClass);
EducationClass = null;
}
return List_EducationClass;
}
大家帮改下?
EnForGrass 2012-04-24
  • 打赏
  • 举报
回复
仔细调试一下RunProc这个函数,看看存储过程在sql中能否返回相应的数据
orochiheart 2012-04-24
  • 打赏
  • 举报
回复
rec.Read()出错了 看看rec是不是null?
donet菜鸟 2012-04-24
  • 打赏
  • 举报
回复
未将对象引用设置到对象的实例。

出现此问题,通常是null值
spidershark 2012-04-24
  • 打赏
  • 举报
回复
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程的名称</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateProcCommand(procName, null);
try
{
///执行存储过程
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
}
finally
{
///关闭数据库的连接
Close();
}
spidershark 2012-04-24
  • 打赏
  • 举报
回复
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EducationClass]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[EducationClass]
GO

CREATE TABLE [dbo].[EducationClass] (
[ClassID] [int] IDENTITY (1, 1) NOT NULL ,
[ParentID] [int] NULL ,
[ClassName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[ParentOrder] [int] NULL
) ON [PRIMARY]
GO




CREATE PROCEDURE [dbo].[Get_EducationClass]
(
@ParentID int
)
AS
SELECT * FROM EducationClass WHERE ParentID=@ParentID ORDER BY ParentOrder



GO

表和存储过程如下

ohkuy 2012-04-24
  • 打赏
  • 举报
回复
你检查 sqlHelper.RunProc这个方法,
out rec为null了
bdmh 2012-04-24
  • 打赏
  • 举报
回复
那就检查 RunProc代码,看看rec什么情况下会得不到值,为null,访问出错
ohkuy 2012-04-24
  • 打赏
  • 举报
回复

你要按照流程来,
先打开连接,
再执行命令
再关闭连接
try
{
//打开连接
...
///读取数据
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
dataReader = null;
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
}
finally
{
关闭连接
}
看你代码每次都没关闭连接
spidershark 2012-04-24
  • 打赏
  • 举报
回复
[Exception: ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。]
SQLHelper.SQLHelper.RunProc(String procName, SqlParameter[] prams, SqlDataReader& dataReader) in G:\2012-4\山西管理职业学院网站程序\SQLHelper\SQLHelper.cs:174
DAL.OccupationClassSQL.Get_OccupationClass(Int32 nParentID) in G:\2012-4\山西管理职业学院网站程序\DAL\OccupationClassSQL.cs:92

[Exception: ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。]
DAL.OccupationClassSQL.Get_OccupationClass(Int32 nParentID) in G:\2012-4\山西管理职业学院网站程序\DAL\OccupationClassSQL.cs:97
BLL.OccupationClassSystem.Get_OccupationClass(Int32 nParentID) in G:\2012-4\山西管理职业学院网站程序\BLL\OccupationClassSystem.cs:50
MasterPage.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\sxglzyxy\web\MasterPage.master.cs:42
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Control.LoadRecursive() +131
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061



我的在IIS就会出现这问题,直接编译不会出现问题,连续点击七次以上就会出现问题
spidershark 2012-04-24
  • 打赏
  • 举报
回复
ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Exception: ExecuteReader 要求已打开且可用的连接。连接的当前状态为已关闭。

源错误:


行 172: {
行 173: SystemError.CreateErrorLog(ex.Message);
行 174: throw new Exception(ex.Message);
行 175: }
行 176: }

ohkuy 2012-04-24
  • 打赏
  • 举报
回复
你要将异常信息抛出来,而不是吃掉它

catch(Exception ex)
{
//dataReader = null;
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message); //再看异常信息是什么?
}
spidershark 2012-04-24
  • 打赏
  • 举报
回复
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
///创建Command
SqlCommand cmd = CreateProcCommand(procName, prams);

try
{
///读取数据
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch(Exception ex)
{
dataReader = null;
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧