Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi all,
in my gridview ,i was not able to perform delete operation .here is the code.

public void delete(string name)
        {

            using (SqlConnection con = new SqlConnection(@"Data Source=SATEESH\SQLEXPRESS;Initial Catalog=demo;Integrated Security=True"))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand("sp_delete", con))
                {

                    cmd.Parameters.AddWithValue("@name", name);

                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.ExecuteNonQuery();
                                }
                        
            }
        
        }

in ROW DELETING EVENT i called that method.
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           string name = GridView1.Rows[e.RowIndex].Cells[0].Text;
           delete(name);
           bindgridview();
       }


the stored procedure is
SQL
create proc sp_update
@name varchar(max),
@email varchar(max),
@phone int
as
begin
update register
set
email=@email,
phone=@phone
where name=@name
end;

everything is working fine ,no error, but the operation is unable to perform.in debug mode i checked and found out string name="".i dont know why its becoming null
Posted
Updated 27-Apr-12 7:44am
v3
Comments
[no name] 27-Apr-12 13:43pm    
Format code snippets when posting

Debug again and verify you are getting the correct row and cell. You can also get a reference to the item through the GridViewDeleteEventArgs and find the control like this:

e.Item.FindControl("my control")


See this example for more DataGridDemo[^]
 
Share this answer
 
Comments
satishmachineni 27-Apr-12 13:50pm    
thanks mark i will check it out
satishmachineni 27-Apr-12 13:52pm    
i didnt made anay column as primary key , after making one column as primary key , i got answer.thanks for replying and for your time
[no name] 27-Apr-12 17:24pm    
You're welcome
where is your SQL query for Deletion? (Sp_Delete)?did nt see it here.and if you want to update the grid then use raw updating event.


if you want to delete, In ur stored procedure check the syntax is correct.
Example:
in stored p:

BEGIN

DELETE FROM register WHERE register.name = @name;

END
 
Share this answer
 
v2
Comments
[no name] 27-Apr-12 17:24pm    
Did even try to read and understand the question?

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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