获取“索引超出范围。必须是非负数且小于集合的大小。”

时间:2017-11-14 09:21:57

标签: c# asp.net

我试图通过网格视图删除数据库中的行,但是,我收到此错误:“索引超出范围。必须是非负数且小于集合的大小。”

 protected void ClientDataGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
    String strConnString = ConfigurationManager.ConnectionStrings["PADSConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);    
    string client_code = ClientDataGrid.DataKeys[e.RowIndex].Values["client_code"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("delete from client where client_code=" + client_code, con);
    int result = cmd.ExecuteNonQuery();
    con.Close();
    if (result == 1)
    {
        ClientDataGrid.DataBind();
        Label1.Text = "Deleted successfully";
    }
}

protected void ClientDataGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{       
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string client_code = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "client_code"));
        Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete");
        if (lnkbtnresult != null)
        {
            lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" + client_code + "')");
        }
    }

}

专门在此行中获取错误

    string client_code = 
    ClientDataGrid.DataKeys[e.RowIndex].Values["client_code"].ToString();

0 个答案:

没有答案