注册 登录
编程论坛 ASP.NET技术论坛

System.ArgumentOutOfRangeException异常错误

水晶之恋 发布于 2007-06-02 17:21, 807 次点击

public void DataList_DeleteCommand(object source, System.Web.UI.WebControls.DataListCommandEventArgs e)
{
aa=Convert.ToString(DataList1.DataKeys[e.Item.ItemIndex]);
//System.ArgumentOutOfRangeException:索引超出范围,必须为非负值并小于集合大小.参数名: index

string str="select count(*) from users where users.dep_id='" + aa + "'";
com=new SqlCommand(str,conn);
conn.Open();
int Count=Convert.ToInt32(com.ExecuteScalar());
if(Count==0)
{
Delete();

}
else Response.Write("<script>javascript:alert('该部门里还有员工,不允许删除!!!');window.location='dep_manage.aspx'</script>");

conn.Close();

}


private void Delete()
{
string sql="delete from dep where dep_id='" + aa + "'";
com1=new SqlCommand(sql,conn);

com1.ExecuteNonQuery();
conn.Close();
ListBind();
}

[此贴子已经被作者于2007-6-2 17:36:49编辑过]

8 回复
#2
skyland842007-06-02 17:28
你怎么不把出错信息发出来看看?
#3
GrimFish2007-06-02 17:49
饿,看来这篇帖子你的思路终于明白了,这个是DATAKEYS没有设置

DataList1.DataKeyField = "dep_id";//你这里应该是设置为

或者在DataList1,右键,属性。找到DataKeyField,设置为这个字段。
#4
水晶之恋2007-06-02 17:52

我在前台已经设置了
<asp:datalist DataKeyField = "dep_id"/>

#5
GrimFish2007-06-02 18:00

暂时看不出,跟踪调试一下。在什么情况出现错误?
string str="select count(*) from users where users.dep_id='" + aa + "'";
这句改一下
string str="select * from users where users.dep_id='" + aa + "'";

#6
skyland842007-06-02 18:05
e.Item.ItemIndex

这个如果不是 SELECTED 的index 的话 那e.Item.ItemIndex 的4值就是-1

你这里可能的情况就是这样了!
#7
GrimFish2007-06-02 18:07
DataList1.DataKeys
LS的,看别人的代码不应该乱看吧。

DataList1.DataKeys[e.Item.ItemIndex]

这里是不很明确了是DataList1.DataKeys?

这里不是很明确的说明了DataList_DeleteCommand事件?

System.Web.UI.WebControls.DataListCommandEventArgs e

难道这个还需要解释?
#8
水晶之恋2007-06-02 18:09
我不是很明白skyland84的意思
#9
skyland842007-06-02 18:17
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView1.SelectedIndex=e.RowIndex;////你加上这段看看
int number = (int)GridView1.SelectedValue;
AccessDataSource1.DeleteParameters["pid"].DefaultValue = number.ToString();
AccessDataSource1.Delete();
//Response.Redirect("admin.aspx");
GridView1.DataSource = AccessDataSource1;
GridView1.DataBind();
}


这是我 之前写的代码!
类似处理
1