asp.net Repeater 第二次绑定出现 “未将对象引用设置到对象的实例。”

u010393040 2014-10-14 11:32:59

public void BindrepStudentList(int num)
{
DataTable dt = StudentBll.GetInfoByNum(num);
this.repStudentList.DataSource = dt;
this.repStudentList.DataBind();
dt = null;
}
这里的参数num是下面sql 语句用到的start
public DataTable GetInfoByNum(int start)
{
if (start > 1)
return SqlDbHelper.ExecuteDataTable(@"select top 5 stuID,stuName,stuSex,IdentificationNumber,className,stuYear from tb_Student s inner join tb_Class c
on s.classID = c.classID where stuID>all(select top " + (start - 1) + " stuID from tb_Student order by stuID asc) ");
else
{
return SqlDbHelper.ExecuteDataTable(@"select top 5 stuID,stuName,stuSex,IdentificationNumber,className,stuYear from tb_Student s inner join tb_Class c
on s.classID = c.classID ");
}
}
第一次加载数据没问题,关键是第二次翻页的时候,我是使用jq用ajax跳转到一般处理程序加载数据的
private void DoLoadData()
{
string strStartNum = context.Request.Params["startNum"];
if (!PageHelper.IsNum(strStartNum))
{
AjaxMsgHelper.AjaxMsg("error", "Ajax传值,传向StudentManager.ashx页面的参数异常.");
}
else
{
Web.Manager.View.StudentManager StudentManager = new View.StudentManager();
StudentManager.BindrepStudentList(int.Parse(strStartNum));
}
}
就这样又调用最上面的方法 ,然后就出错了
<html>
<head>
<title>未将对象引用设置到对象的实例。</title>
<meta name="viewport" content="width=device-width" />
<style>
body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;}
p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
.marker {font-weight: bold; color: black;text-decoration: none;}
.version {color: gray;}
.error {margin-bottom: 10px;}
.expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
@media screen and (max-width: 639px) {
pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
}
@media screen and (max-width: 479px) {
pre { width: 280px; }
}
</style>
</head>

<body bgcolor="white">

<span><H1>“/”应用程序中的服务器错误。<hr width=100% size=1 color=silver></H1>

<h2> <i>未将对象引用设置到对象的实例。</i> </h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

<b> 说明: </b>执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

<br><br>

<b> 异常详细信息: </b>System.NullReferenceException: 未将对象引用设置到对象的实例。<br><br>

<b>源错误:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>

行 21: {
行 22: DataTable dt = StudentBll.GetInfoByNum(num);
<font color=red>行 23: this.repStudentList.DataSource = dt;
</font>行 24: this.repStudentList.DataBind();
行 25: dt = null;</pre></code>

</td>
</tr>
</table>

<br>

<b> 源文件: </b> e:\论文项目\SkillExamination\SkillExamination.Web\Manager\View\StudentManager.aspx.cs<b>    行: </b> 23
<br><br>

<b>堆栈跟踪:</b> <br><br>

<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>

[NullReferenceException: 未将对象引用设置到对象的实例。]
SkillExamination.Web.Manager.View.StudentManager.BindrepStudentList(Int32 num) in e:\论文项目\SkillExamination\SkillExamination.Web\Manager\View\StudentManager.aspx.cs:23
SkillExamination.Web.Manager.Action.StudentManager.DoLoadData() in e:\论文项目\SkillExamination\SkillExamination.Web\Manager\Action\StudentManager.ashx.cs:67
SkillExamination.Web.Manager.Action.StudentManager.SonLoad() in e:\论文项目\SkillExamination\SkillExamination.Web\Manager\Action\StudentManager.ashx.cs:21
SkillExamination.Web.Manager.Action.AdminBaseHandler.ProcessRequest(HttpContext context) in e:\论文项目\SkillExamination\SkillExamination.Web\Manager\Action\AdminBaseHandler.ashx.cs:23
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
</pre></code>

</td>
</tr>
</table>

<br>

<hr width=100% size=1 color=silver>

<b>版本信息:</b> Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.34212

</font>

</body>
</html>
...全文
269 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010393040 2014-10-16
  • 打赏
  • 举报
回复
加过 还是不对的。 可能是因为在一般处理程序里调用web窗体后台页面类的方法的错。
czfeixiang 2014-10-16
  • 打赏
  • 举报
回复
是不是刷新的时候清空了变量?你在页面加载里面加个ispostback试下!
u010393040 2014-10-16
  • 打赏
  • 举报
回复
引用 11 楼 luanjun123456 的回复:
[quote=引用 10 楼 u010393040 的回复:] [quote=引用 9 楼 luanjun123456 的回复:] [quote=引用 6 楼 u010393040 的回复:] [quote=引用 2 楼 luanjun123456 的回复:] 断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
DataTable有数据 在绑定repeater的DataSource的出现异常[/quote] 你是在一般处理程序中绑定的值,可能和这个有关系,因为服务器控件一般处理数据都要在if(!this.IsPostBack){}中才会正常[/quote] 我也想过这种可能,但是不知道怎么修改。[/quote] 实际上你就是想无刷新显示列表数据,那么你只能放弃使用GridView,因为这东西本身就是跑在服务器端的控件,最好的方式还是自己手写Table,在一般处理程序中返回Json,前台获取数据并拼接页面[/quote] 我是想写分页,结果没法加载数据,现在只能用table自己一点点写了。不过还是很谢谢你。
完美的神速 2014-10-16
  • 打赏
  • 举报
回复
引用 10 楼 u010393040 的回复:
[quote=引用 9 楼 luanjun123456 的回复:] [quote=引用 6 楼 u010393040 的回复:] [quote=引用 2 楼 luanjun123456 的回复:] 断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
DataTable有数据 在绑定repeater的DataSource的出现异常[/quote] 你是在一般处理程序中绑定的值,可能和这个有关系,因为服务器控件一般处理数据都要在if(!this.IsPostBack){}中才会正常[/quote] 我也想过这种可能,但是不知道怎么修改。[/quote] 实际上你就是想无刷新显示列表数据,那么你只能放弃使用GridView,因为这东西本身就是跑在服务器端的控件,最好的方式还是自己手写Table,在一般处理程序中返回Json,前台获取数据并拼接页面
u010393040 2014-10-16
  • 打赏
  • 举报
回复
引用 9 楼 luanjun123456 的回复:
[quote=引用 6 楼 u010393040 的回复:] [quote=引用 2 楼 luanjun123456 的回复:] 断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
DataTable有数据 在绑定repeater的DataSource的出现异常[/quote] 你是在一般处理程序中绑定的值,可能和这个有关系,因为服务器控件一般处理数据都要在if(!this.IsPostBack){}中才会正常[/quote] 我也想过这种可能,但是不知道怎么修改。
完美的神速 2014-10-16
  • 打赏
  • 举报
回复
引用 6 楼 u010393040 的回复:
[quote=引用 2 楼 luanjun123456 的回复:] 断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
DataTable有数据 在绑定repeater的DataSource的出现异常[/quote] 你是在一般处理程序中绑定的值,可能和这个有关系,因为服务器控件一般处理数据都要在if(!this.IsPostBack){}中才会正常
u010393040 2014-10-15
  • 打赏
  • 举报
回复
引用 4 楼 u012175253 的回复:
单独调试一步一步往下看吧。肯定有一个值为空了。
public void BindrepStudentList(int num) { DataTable dt = StudentBll.GetInfoByNum(num); this.repStudentList.DataSource = dt; this.repStudentList.DataBind(); dt = null; } 这里的dt是有数据的。
u010393040 2014-10-15
  • 打赏
  • 举报
回复
引用 5 楼 ayanamireizero 的回复:
public void BindrepStudentList(int num) { DataTable dt = StudentBll.GetInfoByNum(num); this.repStudentList.DataSource = dt; this.repStudentList.DataBind(); dt = null; } 这里的dt为null了, if(dt!=null) { this.repStudentList.DataSource = dt; this.repStudentList.DataBind(); } 这样不会报错,但是还是不能翻页.
会不会是因为repeater的DataSource没有清空,不能再次绑定?
u010393040 2014-10-15
  • 打赏
  • 举报
回复
引用 2 楼 luanjun123456 的回复:
断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
DataTable有数据 在绑定repeater的DataSource的出现异常
ayanamireizero 2014-10-15
  • 打赏
  • 举报
回复
public void BindrepStudentList(int num) { DataTable dt = StudentBll.GetInfoByNum(num); this.repStudentList.DataSource = dt; this.repStudentList.DataBind(); dt = null; } 这里的dt为null了, if(dt!=null) { this.repStudentList.DataSource = dt; this.repStudentList.DataBind(); } 这样不会报错,但是还是不能翻页.
情若能自控Q 2014-10-15
  • 打赏
  • 举报
回复
单独调试一步一步往下看吧。肯定有一个值为空了。
myhope88 2014-10-15
  • 打赏
  • 举报
回复
程序逻辑有问题
完美的神速 2014-10-15
  • 打赏
  • 举报
回复
断点调试看看你的GetInfoByNum方法中的 if (start > 1)情况下的返回的DataTable是否为空,如果为空,那就是sql语句写的有问题。
淡淡的活着 2014-10-15
  • 打赏
  • 举报
回复
调试看看第二次哪里为空呗·

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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