String strConnection=ConfigurationSettings.AppSettings["connstring"];
SqlConnection myConnection=new SqlConnection(strConnection);
String deleteCmd = "DELETE from it where id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.Text= "<b>Record Deleted</b><br>" + deleteCmd;
// Message.InnerHtml = "<b>Record Deleted</b><br>" + deleteCmd;
}
catch (SqlException)
{
Message.Text= "ERROR: Could not delete record";
Message.Style["color"] = "red";
} myCommand.Connection.Close(); BindGrid();
这是出的错误
“/it”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误:
行 130: SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
行 131: myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
行 132: myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
行 133:
行 134: myCommand.Connection.Open();
源文件: c:\inetpub\wwwroot\it\show.aspx.cs 行: 132
SqlConnection myConnection=new SqlConnection(strConnection);
String deleteCmd = "DELETE from it where id = @Id"; SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
{
myCommand.ExecuteNonQuery();
Message.Text= "<b>Record Deleted</b><br>" + deleteCmd;
// Message.InnerHtml = "<b>Record Deleted</b><br>" + deleteCmd;
}
catch (SqlException)
{
Message.Text= "ERROR: Could not delete record";
Message.Style["color"] = "red";
} myCommand.Connection.Close(); BindGrid();
这是出的错误
“/it”应用程序中的服务器错误。
--------------------------------------------------------------------------------索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index源错误:
行 130: SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
行 131: myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
行 132: myCommand.Parameters["@Id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex];
行 133:
行 134: myCommand.Connection.Open();
源文件: c:\inetpub\wwwroot\it\show.aspx.cs 行: 132
解决方案 »
- 散分讨论主流源代码版本控制软件发展趋势
- 急!如何检查一个数在两个大数范围之内呀?
- asp.net 如何编辑图片,在图片中划线写字
- [菜鸟]C#函数间传递的参数最大长度是多少?如果解决传递10万长度参数的问题?在线等
- 急求
- 关于SQL2005Reporting Service 与.NET2.0程序的整合
- 如何设置虚拟目录?
- 服务上出Server Error in '/' Application
- 授权问题(2000分)
- 用 Visual Studio.NET b2 开发 ASP.NET 的实战问题????????????
- 如何获取COMPUTE BY分析结果
- 网站项目,新闻发布系统类,asp.net,最好是C#脚本,有兴趣的请进!
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[it]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[it]
GOCREATE TABLE [dbo].[it] (
[itID] [int] IDENTITY (1, 1) NOT NULL ,
[itname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[itdep] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[itlianxi] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[itwenti] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int, 11));错误的地方
myCommand.Parameters["@Id"].Value = (int) DataGrid1.DataKeys[e.Item.ItemIndex];
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %><html><head> <script runat="server"> ICollection CreateDataSource()
{
// Create sample data for the DataGrid control.
DataTable dt = new DataTable();
DataRow dr;
// Define the columns of the table.
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double))); // Define the primary key for the table as the IntegerValue
// column (column 0). To do this, first create an array of
// DataColumns to represent the primary key. The primary key can
// consist of multiple columns, but in this example, only
// one column is used.
DataColumn[] keys = new DataColumn[1];
keys[0] = dt.Columns[0]; // Then assign the array to the PrimaryKey property of the DataTable.
dt.PrimaryKey = keys;
// Populate the table with sample values.
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dt.Rows.Add(dr);
} // To persist the data source between posts to the server,
// store it in session state.
Session["Source"] = dt;
DataView dv = new DataView(dt);
return dv; }
void Page_Load(Object sender, EventArgs e)
{
// Load sample data only once, when the page is first loaded.
if (!IsPostBack)
{
ItemsGrid.DataSource = CreateDataSource();
ItemsGrid.DataBind();
} } void Delete_Command(Object sender, DataGridCommandEventArgs e)
{ // Retrieve the data table from session state.
DataTable dt = (DataTable)Session["Source"]; // Retrieve the data row to delete from the data table.
// Use the DataKeys property of the DataGrid control to get
// the primary key value of the selected row.
// Search the Rows collection of the data table for this value.
DataRow row;
row = dt.Rows.Find(ItemsGrid.DataKeys[e.Item.ItemIndex]); // Delete the item selected in the DataGrid from the data source.
if(row != null)
{
dt.Rows.Remove(row);
} // Save the data source.
Session["Source"] = dt; // Create a DataView and bind it to the DataGrid control.
DataView dv = new DataView(dt);
ItemsGrid.DataSource = dv;
ItemsGrid.DataBind(); } </script></head><body> <form runat="server"> <h3>BaseDataList DataKeys Example</h3> <asp:DataGrid id="ItemsGrid"
BorderColor="Black"
ShowFooter="False"
CellPadding=3
CellSpacing="0"
HeaderStyle-BackColor="#aaaadd"
DataKeyField="IntegerValue"
OnDeleteCommand="Delete_Command"
runat="server"> <Columns> <asp:ButtonColumn Text="Delete"
CommandName="Delete"/> </Columns> </asp:DataGrid> </form></body>
</html>
你自己检查一下你的代码
跟踪一下
看看 当前的 e.Item.ItemIndex 是多少
使用DataKeys属性前需要指定DataKeys对应的Datagrid 列需要怎么弄?