gridview更新错误 指定的参数已超出有效值的范围。参数名:index

seles 2008-10-03 11:17:00
更新错误,不知道为什么,下面是详细信息:
用户代码未处理 System.ArgumentOutOfRangeException
Message="指定的参数已超出有效值的范围。\r\n参数名: index"
Source="System.Web"
ParamName="index"
StackTrace:
在 System.Web.UI.ControlCollection.get_Item(Int32 index)
在 admin_Message_management.GridView1_RowUpdating(Object sender, GridViewUpdateEventArgs e) 位置 f:\myweb\admin\Message management.aspx.cs:行号 50
在 System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e)
在 System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
在 System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
在 System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
在 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
在 System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
在 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
在 System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
在 System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
在 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


更新代码如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
string Zhuti = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
string Qq = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
string Email= ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
string Content = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
string Time=((TextBox )(GridView1.Rows [e.RowIndex ].Cells[6].Controls [0])).Text .ToString ().Trim();
SqlConnection sqlcon=new SqlConnection ();
sqlcon .ConnectionString ="server=(local);uid=sa;pwd=sa;database=post";
sqlcon .Open ();
string sqlstr = "update post set name='" +Name + "',zhuti='"+ Zhuti + "',qq='"+Qq + "' ,email='"+Email +"',content='"+Content +"',time='" +Time +"' where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";

SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcom.ExecuteNonQuery();
GridView1.EditIndex = -1;
sqlcon.Close();
DataBind();


}
请各位大侠给我看看,我都快疯了。
...全文
574 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sssddfff 2009-12-29
  • 打赏
  • 举报
回复
那应该怎么改一下呢???
longyong712 2008-11-04
  • 打赏
  • 举报
回复
单元格的索引有错
amandag 2008-10-04
  • 打赏
  • 举报
回复
GridView是否设置了DataKeyNames属性为表的主键

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" >
seles 2008-10-04
  • 打赏
  • 举报
回复
DataKeyNames="id"已设,看问题就出在string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); 这一句
cooolchen 2008-10-03
  • 打赏
  • 举报
回复
name='" +Name + "',zhuti='"+ Zhuti + "',qq='"+Qq + "' ,email='"+Email +"',content='"+Content +"',time='" +Time +"'
---
跟踪这些变量是否能取到值,要么是e.rowindex,要么是cell里面的索引超出范围。
cooolchen 2008-10-03
  • 打赏
  • 举报
回复
这个看代码看不出什么问题的。建议在string name 处加断点,看看e.RowIndex的值。

111,095

社区成员

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

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

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