ASP.NET Gridview数据库绑定支持增删改,记得要完整实现

1.错误情况

/WebSite3”应用程序中的服务器错误。

指定的参数已超出有效值的范围。
参数名: index

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

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

源错误: 

行 26:     {
行 27:         MyClass.Alert(e.RowIndex.ToString()+"1111111");   
行 28:         string id = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim();
行 29:         string fatherID = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
行 30:         string thisTitle = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();

源文件: g:\备份\Visual Studio 2010\WebSites\WebSite3\Admin\MenuManage.aspx.cs    行: 28 

堆栈跟踪: 

[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index]
   System.Web.UI.ControlCollection.get_Item(Int32 index) +9601258
   Admin_MenuManage.GvMenu_RowUpdating(Object sender, GridViewUpdateEventArgs e) in g:\备份\Visual Studio 2010\WebSites\WebSite3\Admin\MenuManage.aspx.cs:28
   System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e) +122
   System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +792
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +159
   System.Web.UI.WebControls.Button.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) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.17929




2.解决办法


 public void InitPage()
    {
        menu_tbBll menuBll = new menu_tbBll();
        GvMenu.DataSource = menuBll.GetList();
        GvMenu.DataKeyNames = new string[] { "ID" };
        GvMenu.DataBind();
    }

 protected void GvMenu_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Response.Write(MyClass.Alert(e.RowIndex.ToString()+"1111111"));
        string id = this.GvMenu.DataKeys[e.RowIndex].Value.ToString();
        string fatherID = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim();
        string thisTitle = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        string thisUrl = ((TextBox)(GvMenu.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
        string sql_str = "update menu_tb set PID=" + fatherID + ", Title='" + thisTitle + "', Urls='" + thisUrl + "'";
        OleDbCommand sqlcom = new OleDbCommand();
        sqlcom.Connection = MyClass.Open();
        sqlcom.CommandText = sql_str;
        int i = sqlcom.ExecuteNonQuery();
        if (i > 0)
        {
            MyClass.Alert("更新成功");
        }
        else
        {
            MyClass.Alert("更新失败");
        }
        MyClass.Close();
        GvMenu.EditIndex = -1;
        this.InitPage();
    
    }

posted @ 2015-03-21 16:17  shugen  阅读(158)  评论(0编辑  收藏  举报