首页蓝耳朵|小小蓝耳朵广州图书馆外文室英语儿童读物资源介绍网佛教青年之友旧版收集永硕E盘Phonics Short Vowels Game 
System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围
所属栏目:ASP.NET(webform)  时间:2009-10-28 15:24  作者:狼子

使用GridView删除数据,调用的是SqlDataSource的删除方法,这个删除方法是一个存储过程,里面判断了数据是不是正在使用,删除了两个相关表的记录

数据表是两个int字段组合的主键,没有外键,写存储过程的方法、写asp.net页面的方法,用的全部和以前做类似操作的时候的方法一样子,就是,这一次,删除报错:

指定的参数已超出有效值的范围。
参数名: value
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: value

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 

堆栈跟踪:

[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: value]
   System.Web.UI.WebControls.GridView.set_SelectedIndex(Int32 value) +1352651
   System.Web.UI.WebControls.GridView.HandleDeleteCallback(Int32 affectedRows, Exception ex) +368
   System.Web.UI.DataSourceView.Delete(IDictionary keys, IDictionary oldValues, DataSourceViewOperationCallback callback) +137
   System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +714
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

有好多人和我有相同的出错,这个是其中一个帖子:http://forums.asp.net/p/974611/1268344.aspx

解决的办法是,在存储过程里,开始的时候,设置“set nocount on”,结束的时候,设置“set nocount off”

出错的原因,网上大家的解释是:

This is probabably beacuse the GridView appears to use the return information of the stored procedure to decide what action to take on re-render.

 If you are using stored procedures which update multiple tables or fires triggers this will effect the @@ROWCOUNT value which seems to trip up the rendering of the GridView when deleting the last item.

还有一些别的人的说法,反正就是说存储过程在删除数据的时候,因为更新了多个表,影响了@@rowcount的值,然后呢,删除完成,GridView重新绑定数据的时候,它想要把SelectedIndex的值设置成一个不正确的值,可能是-2,可能是大于现在行号的值

就是,为什么我在做另一个页面的时候,一点问题都没有呢?我把两个页面的源码对比了,我对比了四个数据表的结构,我对比了两个存在过程的代码,就是字段名和表名不同,别的全部一样子。。。

为什么相同的方法,一个页面可以正确执行,另一个页面出现这种错误呢?

小楼宝宝的涂鸦花花(Imitater)的博客起名称骨测字皖ICP备06000023号-17