| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1065 人关注过本帖
标题:索引超出范围。必须为非负值并小于集合大小。参数名: index,为什么呀?
只看楼主 加入收藏
enthusiasm
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-7-9
收藏
 问题点数:0 回复次数:4 
索引超出范围。必须为非负值并小于集合大小。参数名: index,为什么呀?

我用datagrid做了一个数据库的连接实例,为什么删除某一行的时候不管用呀。错误信息是:

索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
源错误:
行 46: myCommand = new System.Data.SqlClient.SqlCommand(deleteCmd, sqlConnection1);
行 47: myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Id", SqlDbType.NVarChar,11));
行 48: myCommand.Parameters["@Id"].Value =this.DataGrid1.DataKeys[(int)e.Item.ItemIndex];
行 49: myCommand.Connection.Open();


后台代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace emp
{
/// <summary>
/// delete 的摘要说明。
/// </summary>
public class delete : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.Label Message;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand myCommand;


private void Page_Load(object sender, System.EventArgs e)
{
sqlConnection1= new System.Data.SqlClient.SqlConnection("server=QQQQ;database=firminformation;Trusted_Connection=yes");
if (!IsPostBack)
BindGrid();

}
public void BindGrid()
{
sqlDataAdapter1= new System.Data.SqlClient.SqlDataAdapter("select * from employee", sqlConnection1);

DataSet ds = new DataSet();
sqlDataAdapter1.Fill(ds,"employee");

DataGrid1.DataSource=ds.Tables["employee"].DefaultView;
DataGrid1.DataBind();
}
public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "DELETE from employee where e_id = @Id";
myCommand = new System.Data.SqlClient.SqlCommand(deleteCmd, sqlConnection1);
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Id", SqlDbType.NVarChar,11));
myCommand.Parameters["@Id"].Value =this.DataGrid1.DataKeys[(int)e.Item.ItemIndex];
myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
Message.Text = "<b>已删除记录</b><br>" + deleteCmd;
}
catch (System.Data.SqlClient.SqlException)
{
Message.Text = "错误:未能删除记录";
Message.Style["颜色"] = "红色";
}

myCommand.Connection.Close();

BindGrid();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}


帮帮我,好吗谢谢呀!

[此贴子已经被作者于2006-8-21 8:08:05编辑过]

搜索更多相关主题的帖子: 负值 索引 index 堆栈 数据库 
2006-08-21 08:05
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
myCommand.Parameters["@Id"].Value =this.DataGrid1.DataKeys[(int)e.Item.ItemIndex];
把这句里的(int)去掉,再试试呢.

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-21 08:50
enthusiasm
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2006-7-9
收藏
得分:0 

谢谢mylover624,我去过的,不行呀,刚才在群上见个高人指点了一下,

就好了。


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace emp
{
/// <summary>
/// delete 的摘要说明。
/// </summary>
public class delete : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.Label Message;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand myCommand;


private void Page_Load(object sender, System.EventArgs e)
{
sqlConnection1= new System.Data.SqlClient.SqlConnection("server=QQQQ;database=firminformation;Trusted_Connection=yes");
if (!IsPostBack)
BindGrid();

}

public void BindGrid()
{
sqlDataAdapter1= new System.Data.SqlClient.SqlDataAdapter("select * from employee", sqlConnection1);

DataSet ds = new DataSet();
sqlDataAdapter1.Fill(ds,"employee");

DataGrid1.DataSource=ds.Tables["employee"].DefaultView;
//e_id是employee表里的一个字段,根据此字段来删除行的。
DataGrid1.DataKeyField = "e_id";

DataGrid1.DataBind();
}
public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd = "DELETE from employee where e_id = @Id";
myCommand = new System.Data.SqlClient.SqlCommand(deleteCmd, sqlConnection1);
myCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Id", SqlDbType.NVarChar,11));
myCommand.Parameters["@Id"].Value=this.DataGrid1.DataKeys[(int)e.Item.ItemIndex].ToString();
myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
Message.Text = "<b>此条记录已被删除!</b><br>";

}
catch (System.Data.SqlClient.SqlException)
{
Message.Text = "错误:未能删除记录!";
Message.Style["颜色"] = "红色";
}

myCommand.Connection.Close();

BindGrid();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

2006-08-21 10:11
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
晕哦,你没有设置 DataKeyField 啊,呵呵.

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-21 10:19
tel1982
Rank: 3Rank: 3
来 自:水星
等 级:新手上路
威 望:8
帖 子:852
专家分:0
注 册:2006-10-21
收藏
得分:0 

为什么我的与上面的情况一样,但是我按照上面所说的做了还是不行。
这是我的代码:
private void databindTodg()
{
SqlConnection con=new SqlConnection(Application["conStr"].ToString());
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
string str="select * from teaReg";
sda.SelectCommand=new SqlCommand(str,con);
DataSet ds=new DataSet();
sda.Fill(ds,"teacherList");
this.dgTeacher.DataSource=ds.Tables["teacherList"].DefaultView;
this.dgTeacher.DataKeyField="teaID";
this.dgTeacher.DataBind();
sda.Dispose();
con.Close();
con.Dispose();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dgTeacher.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgTeacher_PageIndexChanged);
this.dgTeacher.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTeacher_CancelCommand);
this.dgTeacher.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTeacher_EditCommand);
this.dgTeacher.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTeacher_UpdateCommand);
this.dgTeacher.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgTeacher_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void dgTeacher_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgTeacher.CurrentPageIndex=e.NewPageIndex;
this.databindTodg();
}

private void dgTeacher_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int strID=Convert.ToInt32(e.Item.Cells[0].Text);
SqlConnection con=new SqlConnection(Application["conStr"].ToString());
con.Open();
Response.Write("<script>window.confirm('确实要删除吗?')</script>");
string str="delete from teaReg where teaID="+strID;
SqlCommand cmd=new SqlCommand(str,con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
this.databindTodg();
}

private void dgTeacher_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.dgTeacher.EditItemIndex=e.Item.ItemIndex;
this.databindTodg();
}

private void dgTeacher_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.dgTeacher.EditItemIndex=-1;
this.databindTodg();
}

private void dgTeacher_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// int teacherID=this.dgTeacher.DataKeys[e.Item.ItemIndex].ToString();
int teacherID=Convert.ToInt32(e.Item.Cells[0].Text);
string teacherZc=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string teacherZw=((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string teacherDepartment=((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string teacherEmail=((TextBox)(e.Item.Cells[5].Controls[0])).Text;
SqlConnection con=new SqlConnection(Application["conStr"].ToString());
con.Open();
string strUpdata="updata teaReg set teaZc='"+teacherZc+"',teaZw='"+teacherZw+"',teaDepartment='"+teacherDepartment+"',teaEmail='"+teacherEmail+"' where teaID="+teacherID;
SqlCommand cmd=new SqlCommand(strUpdata,con);
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
con.Dispose();
this.dgTeacher.EditItemIndex=-1;
this.databindTodg();
}
帮忙看一下大家。谢谢了先。


Face up to life with smiles no matter what happens
2007-04-23 14:13
快速回复:索引超出范围。必须为非负值并小于集合大小。参数名: index,为什么呀 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050771 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved