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没有?
最好代码贴全点
[解决办法]
/// <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); }