Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Similar type of problem but this time for update/delete using stored procedures in gridview:

SQL
<asp:GridView runat="server" ID="gv1" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
           OnRowEditing="edit" OnRowUpdating="update" OnRowDeleting="delete" <asp:GridView>


Code(.cs):-
public void delete(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection conn = new SqlConnection(str_con);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand("DeleteInfo", conn);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@s_id", SqlDbType.Int, 10, "s_id");//<----s_id is not primary key
            cmd1.ExecuteNonQuery();
            BindData();
            conn.Close();
        }

Stored procedure:-
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[DeleteInfo]
(
    @s_id int
)
as
begin
delete from info where s_id=@s_id
end
Posted
Updated 5-Dec-11 20:56pm
v2

public void delete(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gv1.Rows[e.RowIndex].Cells[4].Text);
            SqlConnection conn = new SqlConnection(str_con);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand("DeleteInfo", conn);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@s_id", SqlDbType.Int).Value = id;//CHANGE IN THIS LINE
            cmd1.ExecuteNonQuery();
            BindData();
            conn.Close();
        }
 
Share this answer
 
Try this one instead:

C#
int idToDelete = 29;
public void delete(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection conn = new SqlConnection(str_con);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand("DeleteInfo", conn);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@s_id", idToDelete);//user with ID 29 will be deleted.
            cmd1.ExecuteNonQuery();
            BindData();
            conn.Close();
        }


Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard
 
Share this answer
 
v3
Comments
07navneet 6-Dec-11 3:25am    
why would i use textbox to delete a row in gridview??
[no name] 6-Dec-11 3:32am    
textbox1 contains the ID that you will be deleting. i updated my answer to make it clearer.
07navneet 6-Dec-11 3:41am    
But I want to delete the row using "AutoGenerateDeleteButton" and don't want to take value from a textbox.
[no name] 6-Dec-11 4:41am    
then find a way to get the id of the selected row then pass it as parameter as show above.
07navneet 6-Dec-11 5:13am    
this much logic I know!! but how to implement(code) it??

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