本文介绍了更新网格数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新网格值时收到一个名为Index Out of Range的错误。请查看以下代码。

  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{

string username = GridView1.DataKeys [e.RowIndex] .Values [ 名称] .ToString(); // 索引超出范围。必须是非负且小于集合的大小参数名称:index(这是错误)
TextBox备注=(TextBox)GridView1.Rows [e.RowIndex] .FindControl( txtRemarks);
string connStr = ConfigurationManager.ConnectionStrings [ myConnectionString]的ConnectionString。
SqlConnection con = new SqlConnection(connStr);

con.Open();
SqlCommand cmd = new SqlCommand( update gridsample set备注=' + Remarks.Text + '其中Name =' + username + ';,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindEmployeeDetails();
}
}
解决方案

I am getting an error called Index Out of Range while updating the grid value. Kindly have a look at the following code.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            string username = GridView1.DataKeys[e.RowIndex].Values["Name"].ToString();//Index was out of range. Must be non-negative and less than the size of the collection Parameter name: index (this is error)
            TextBox Remarks = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtRemarks");
            string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connStr);

            con.Open();
            SqlCommand cmd = new SqlCommand("update gridsample set Remarks='" + Remarks.Text + "'where Name='" + username+"';", con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.EditIndex = -1;
            BindEmployeeDetails();
        }
    }
解决方案


这篇关于更新网格数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-30 04:38