索引超出范围。必须为非负值并小于集合大小。 参数名: index public partial class

说明:执行当前Web请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。异常详细信息:System.ArgumentOut... 说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index
源错误:
行 31: {
行 32: SqlConnection mycon = new SqlConnection("server=ALEXANDER-PC;uid=sa;pwd=123456;database=test");
行 33: SqlCommand mycom = new SqlCommand("delete from gridview where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",mycon);
行 34: mycon.Open();
行 35: mycom.ExecuteNonQuery();
源文件: d:\WebSites\gridview\Default.aspx.cs 行: 33
堆栈跟踪:
[ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index]
System.Collections.ArrayList.get_Item(Int32 index) +7488568
System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) +12
_Default.GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e) in d:\WebSites\gridview\Default.aspx.cs:33
System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +133
System.Web.UI.WebControls.GridView.HandleDelete(GridViewRow row, Int32 rowIndex) +569
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +869
System.Web.UI.WebControls.GridView.RaisePostBackEvent(String eventArgument) +207
System.Web.UI.WebControls.GridView.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

版本信息: Microsoft .NET Framework 版本:2.0.50727.5466; ASP.NET 版本:2.0.50727.5456

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection mycon = new SqlConnection("server=ALEXANDER-PC;uid=sa;pwd=123456;database=test");
SqlDataAdapter myada=new SqlDataAdapter("select*from gridview",mycon);
DataSet ds = new DataSet();
myada.Fill(ds, "c");
GridView1.DataSource = ds.Tables["c"];
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection mycon = new SqlConnection("server=ALEXANDER-PC;uid=sa;pwd=123456;database=test");
SqlCommand mycom = new SqlCommand("delete from gridview where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",mycon);
mycon.Open();
mycom.ExecuteNonQuery();
mycon.Close();
GridView1.DataBind();
}
}
展开
 我来答
youbl
2013-04-17 · TA获得超过2510个赞
知道大有可为答主
回答量:1844
采纳率:62%
帮助的人:1635万
展开全部
错误都告诉你了,在33行出的错:
行 33: SqlCommand mycom = new SqlCommand("delete from gridview where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'",mycon);

也就是DataKeys[e.RowIndex]出错了,
要么e.RowIndex为-1,要么e.RowIndex超过DataKeys的长度了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
TableDI
2024-07-18 广告
在Excel中,使用`VLOOKUP`函数进行单一条件查找非常常见,但`VLOOKUP`本身并不直接支持双重条件。然而,你可以通过一些间接方法实现。一种方法是将两个条件合并为一个标识符(例如,通过连接或串联),然后在另一个区域中查找此标识符... 点击进入详情页
本回答由TableDI提供
百度网友16ad9e1
2013-04-17 · TA获得超过773个赞
知道小有建树答主
回答量:477
采纳率:0%
帮助的人:499万
展开全部
索引越界 打断点在 d:\WebSites\gridview\Default.aspx.cs 行: 33 调试吧 看代码是看不出来的。。。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式