Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.
Posted
Updated 28-Jun-16 19:50pm
Comments
ZurdoDev 28-Jun-16 16:29pm    
There is no reason to show all the sql code. The error is happening in C#. Do you understand what the error is telling you?
Foothill 28-Jun-16 17:11pm    
In your code block the only place I can spot where a collection used is GridView1.DataKeys[e.RowIndex]. Check if the row index in the event data is in the data keys. I would suspect that it is not.
jayveebishie 30-Jun-16 6:59am    
You should learn how to use Breakpoint and check your code line by line

ensure that the GridView markup is having the DataKeyNames [^]attribute
HTML
DataKeyNames="Id"
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900