积极答复者
索引超出范围。必须为非负值并小于集合大小。 参数名: index 这是什么问题啊??

问题
-
出现的错误提示:索引超出范围。必须为非负值并小于集合大小。 参数名: index 指的是 da.DeleteCommand.ExecuteNonQuery();出错
但是我对这个没有认识的,请各位指出错误并修改
public partial class Default6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("App_Data/eshop.mdb.mdb");
string strSel = "select *from 商品类别";
OleDbCommand selcom = new OleDbCommand(strSel, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = selcom;
DataSet ds = new DataSet();
da.Fill(ds, "商品类别");
GridView1.DataSource = ds.Tables["商品类别"].DefaultView;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values);
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("App_Data/eshop.mdb.mdb");
string strDel = "delete from 商品类型 where CategoryID='" + IntClassID;
OleDbCommand delcom = new OleDbCommand(strDel, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
conn.Open();
da.DeleteCommand = delcom;
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
// GridView1_PageIndexChanging:分页
//GridView1_RowDeleting:删除
//GridView1_RowEditing:编辑
//GridView1_RowUpdating:修改
//GridView1_RowCancelingEdit:取消编辑
//GridView1_RowDataBound:数据绑定的一些相关处理
}
}
答案
-
GridView 绑定主键 设置 DataKeyNames="CategoryID" 属性
还有你的删除按钮写在哪里了- 已标记为答案 tdmagicjie 2009年12月26日 15:47
全部回复
-
int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values); //获取主键的ID
string strDel = "delete from 商品类型 where CategoryID='" + IntClassID; //SQL语句
但是运行时,出现
索引超出范围。必须为非负值并小于集合大小。
参数名: index
指的是:int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values);
但是不知道是为什么 -
int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values); //获取主键的ID
这个values是数组不是单值 写value属性
string strDel = "delete from 商品类型 where CategoryID='" + IntClassID; //SQL语句
但是运行时,出现
索引超出范围。必须为非负值并小于集合大小。
参数名: index
指的是:int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values);
但是不知道是为什么 -
int IntClassID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" + "Data Source=" + Server.MapPath("App_Data/eshop.mdb.mdb");
string strDel = "delete from 商品类型 where CategoryID=" + IntClassID;
OleDbCommand delcom = new OleDbCommand(strDel, conn);
OleDbDataAdapter da = new OleDbDataAdapter();
conn.Open();
da.DeleteCommand = delcom;
da.DeleteCommand.ExecuteNonQuery();
conn.Close();
修改后问题仍旧未解决。是否可以尝试另一种方法实行gridview对数据库的数据的删除效果呢? -
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width: 441px; height: 171px">
<tr>
<td style="width: 100px; height: 5px">
商品类别管理:</td>
</tr>
<tr>
<td style="width: 100px; height: 64px">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
OnRowDeleted="GridView1_RowDeleted" Width="432px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CellPadding="2" GridLines="None" HorizontalAlign="Center" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" PageSize="5">
<Columns>
<asp:BoundField DataField="CategoryID" HeaderText="商品种类编号" />
<asp:BoundField DataField="Merchandise" HeaderText="商品种类名称" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</div>
</form>
</body>
</html> -
GridView 绑定主键 设置 DataKeyNames="CategoryID" 属性
还有你的删除按钮写在哪里了- 已标记为答案 tdmagicjie 2009年12月26日 15:47