当前位置: 代码迷 >> ASP.NET >> GridView基于删除异常:索引超出范围。必须为非负值并小于集合大小。参数名index
  详细解决方案

GridView基于删除异常:索引超出范围。必须为非负值并小于集合大小。参数名index

热度:937   发布时间:2013-02-25 00:00:00.0
GridView基于删除错误:索引超出范围。必须为非负值并小于集合大小。参数名index
删除代码如下:
  SqlConnection sqlcon;
  SqlCommand sqlcom;
  string strCon = "server=.;uid=sa;pwd=;database=ASPNET配置";
  protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  {
  // SqlConnection cn = new SqlConnection("server=.;uid=sa;pwd=;database=ASPNET配置");

  string sqlstr = "delete from 表 where UserId='" + GridView1.DataKeys[e.RowIndex].Value.ToString()+ "'";
  sqlcon = new SqlConnection(strCon);
  sqlcom = new SqlCommand(sqlstr, sqlcon);
  sqlcon.Open();
  sqlcom.ExecuteNonQuery();
  sqlcon.Close();
  this.DataBind();
  }

------解决方案--------------------------------------------------------
绑定gridview的时候配置了datakeys没有?
最好代码贴全点
------解决方案--------------------------------------------------------
C# code
 /// <summary>        /// 删除一条数据        /// </summary>        public void Delete(int KID)        {            StringBuilder strSql = new StringBuilder();            strSql.Append("delete from SYS_CYLB ");            strSql.Append(" where KID=@KID ");            SqlParameter[] parameters = {                    new SqlParameter("@KID", SqlDbType.Int,4)};            parameters[0].Value = KID;            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);        }
  相关解决方案