asp.net中gridview更新时出现错误:指定的参数已超出有效值的范围。参数index 50
用户代码未处理System.ArgumentOutOfRangeExceptionMessage="指定的参数已超出有效值的范围。\r\n参数名:index"Source...
用户代码未处理 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();
}
我当然现百度搜过,要能找到我也不会来求教了 展开
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();
}
我当然现百度搜过,要能找到我也不会来求教了 展开
3个回答
展开全部
GridView1.Rows[e.RowIndex] --->你设定了绑定的主键了吗?
例如用dataset绑定的话没有写以下第二句就会出现你的异常。
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
没有设定KEYNAMES你在GridView1.Rows[e.RowIndex] e.RowIndex就是NULL当然越界。
例如用dataset绑定的话没有写以下第二句就会出现你的异常。
GridView1.DataSource = ds.Tables[0];
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataBind();
没有设定KEYNAMES你在GridView1.Rows[e.RowIndex] e.RowIndex就是NULL当然越界。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也遇到过这个问题!
你的问题很明显,你没有设置绑定主键即你的代码中少
string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
这是我的网页中的更新代码,可能对你有帮助(正在使用的代码)
protected void GridView1_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string FAQ_dns = WebConfigurationManager.ConnectionStrings["FAQConnectionString"].ToString();
SqlConnection FAQ_myConn = new SqlConnection(FAQ_dns);
FAQ_myConn.Open();
GridViewRow row = this.GridView1.Rows[e.RowIndex];
SqlCommand scd = new SqlCommand("update Question set QU_type='"
+ ((DropDownList)(GridView1.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1"))).SelectedValue + "'where id=" + id, FAQ_myConn);
scd.ExecuteNonQuery();
Response.Write("<script>alert('修改成功')</script>");
FAQ_myConn.Close();
GridView1.EditIndex = -1;
this.bind();
我更新的是DropDownList空件,但都大同小异你用的是TextBox,仿照这改一下一定有帮助!
你的问题很明显,你没有设置绑定主键即你的代码中少
string uid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
这是我的网页中的更新代码,可能对你有帮助(正在使用的代码)
protected void GridView1_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
string FAQ_dns = WebConfigurationManager.ConnectionStrings["FAQConnectionString"].ToString();
SqlConnection FAQ_myConn = new SqlConnection(FAQ_dns);
FAQ_myConn.Open();
GridViewRow row = this.GridView1.Rows[e.RowIndex];
SqlCommand scd = new SqlCommand("update Question set QU_type='"
+ ((DropDownList)(GridView1.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1"))).SelectedValue + "'where id=" + id, FAQ_myConn);
scd.ExecuteNonQuery();
Response.Write("<script>alert('修改成功')</script>");
FAQ_myConn.Close();
GridView1.EditIndex = -1;
this.bind();
我更新的是DropDownList空件,但都大同小异你用的是TextBox,仿照这改一下一定有帮助!
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
还有一种情况,你把列设为不可编辑了ReadOnly="true"
这也是一种情况
这也是一种情况
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询