这段代码总是给我错误........ system.argumentoutofrangeexception:index超出范围。必须是非负数且小于集合的大小。 [英] This code always give me error........system.argumentoutofrangeexception: index was out of range. Must be non-negative and less than the size of the collection.
本文介绍了这段代码总是给我错误........ system.argumentoutofrangeexception:index超出范围。必须是非负数且小于集合的大小。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
存储过程..........
USE [myDatabase]
GO
/ * *****对象:StoredProcedure [dbo]。[sp_main]脚本日期:28-Jun-16 3:57:04 PM * ***** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo]。[sp_main]
(
@id int = 0 ,
@action varchar( 50 )= NULL,
@firstname varchar( 50 )= NULL,
@lastname varchar( 50 )= NULL,
@email varchar( 50 )= NULL,
@state varchar( 50 )= NULL,
@city varchar( 50 )= NULL,
@mob varchar( 50 )= NULL,
@dep varchar( 50 )= NULL,
@dob date = NULL,
@sala ry int = 0
)
as
begin
if @action = ' insert'
begin
将插入 empDetail值(@ firstname,@ lastname,@ email,@ state,@ city,@ mob,@ default)
声明@empId int
set @empId = SCOPE_IDENTITY()
将插入 empDpartment值(@empId,@ firstname ,@ dep,@ dob,@ salary)
end
if @action = ' delete'
begin
delete from empDetail 其中 id = @id
end
if @ action = ' update'
begin
更新empDetail set fname = @firstname,lname = @lastname,email = @email,state = @state,city = @city,mob = @mob,dep = @ dep 其中 id = @id
update empDpartment set fname = @firstname, department = @ dep,dob = @dob,salary = @salary 其中 empID = @id
end
if @action = ' select'
begin
选择 * 来自 empDetail
end
end
代码用于删除数据........
protected void GridView1_RowDelet ing( object sender,GridViewDeleteEventArgs e)
{
int id = INT 跨度> .Parse(GridView1.DataKeys [e.RowIndex] .Value.ToString());
cmd = new SqlCommand( sp_main ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ id,id);
cmd.Parameters.AddWithValue( @ action, delete);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}< b>< b>< b>< b>< b>< b> < / b > < / b > < / b > < / b > < / b > < / b >
我的尝试:
任何人都可以帮助我....我不知道如何解决这个错误。
解决方案
确保GridView
标记有 DataKeyNames [ ^ ]属性
DataKeyNames =Id
你应该学会尽快使用调试器可能。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。
调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。
调试器 - 维基百科,免费的百科全书 [ ^ ]
掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]
发生错误时,检查集合,您将看到您尝试访问不存在的元素。
Stored Procedure..........
USE [myDatabase]
GO
/****** Object: StoredProcedure [dbo].[sp_main] Script Date: 28-Jun-16 3:57:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[sp_main]
(
@id int = 0,
@action varchar(50) = NULL,
@firstname varchar(50) = NULL,
@lastname varchar(50) = NULL,
@email varchar(50) = NULL,
@state varchar(50) = NULL,
@city varchar(50) = NULL,
@mob varchar(50) = NULL,
@dep varchar(50) = NULL,
@dob date = NULL,
@salary int = 0
)
as
begin
if @action = 'insert'
begin
insert into empDetail values (@firstname, @lastname, @email, @state, @city, @mob, @dep)
declare @empId int
set @empId = SCOPE_IDENTITY()
insert into empDpartment values(@empId, @firstname, @dep, @dob, @salary)
end
if @action = 'delete'
begin
delete from empDetail where id = @id
end
if @action = 'update'
begin
update empDetail set fname = @firstname, lname = @lastname, email = @email, state = @state, city = @city, mob = @mob, dep = @dep where id = @id
update empDpartment set fname = @firstname, department = @dep, dob = @dob, salary = @salary where empID = @id
end
if @action = 'select'
begin
select * from empDetail
end
end
Code for deleting data........
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
cmd = new SqlCommand("sp_main", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@action", "delete");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}<b><b><b><b><b><b></b></b></b></b></b></b>
What I have tried:
Anybody can help me....I don't have an idea, how to solve this error.
解决方案
ensure that theGridView
markup is having the DataKeyNames [^]attribute
DataKeyNames="Id"
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
When the error occurs, check the collection and you will see that you try to access an element that does not exist.
这篇关于这段代码总是给我错误........ system.argumentoutofrangeexception:index超出范围。必须是非负数且小于集合的大小。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文