470,641 Members | 2,555 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Delete in GridView does not work

375 256MB
hello,



i am new to asp.net. i am trying to do a simple application of adding,inserting and deleting rows in gridview. i am using C#.net and javascript.

My edit/update works but i have problems in deleting the rows in the datagrid. i have used command fields(properties-columns) edit,update,cancel and delete commands. i changed it to template field. both are as buttons. ihave changed the id of Delete command to btDelete.

The following is the code for edit and update THIS CODE WORKS
Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowUpdating(object sender,GridViewUpdateEventArgs e) 
  2. {
  3.  
  4. String S1;SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo"); 
  5. MyCon.Open();
  6.  
  7. Button b1 = new Button();
  8.  
  9. TextBox t1 = new TextBox(); 
  10. TextBox t2 = new TextBox();
  11.  
  12. foreach (GridViewRow gv in GridView2.Rows) 
  13. {
  14.  
  15. b1 = (Button)gv.FindControl("button1"); 
  16. t2 = (TextBox)gv.FindControl("TextBox2");
  17.  
  18. t1 = (TextBox)gv.FindControl("TextBox1");if (b1.CommandName == "Update") 
  19. {
  20.  
  21.  
  22.  
  23. S1 = "update t1 set name='" + t2.Text + "' where id=" + t1.Text + ""; 
  24. SqlCommand UpdateCmd = new SqlCommand(S1, MyCon);
  25.  
  26. Response.Write("updated"); 
  27. UpdateCmd.ExecuteNonQuery();
  28.  
  29. }
  30.  
  31. }
  32.  
  33. MyCon.Close();
  34.  
  35.  
  36.  
  37. }
=====================
Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowEditing(Object sender, GridViewEditEventArgs e) 
  2. {
  3.  
  4.  
  5.  
  6. GridView2.EditIndex = e.NewEditIndex;
  7.  
  8. Display();
  9.  
  10. }
  11.  
  12.  
  13.  
  14. public void Display() 
  15. {
  16.  
  17.  
  18.  
  19. SqlConnection GridCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo"); 
  20. GridCon.Open();
  21.  
  22. SqlDataAdapter MyDa = new SqlDataAdapter("select ID,name,date,qty,price,amount from t1", GridCon); 
  23. DataSet MyDs = new DataSet();
  24.  
  25. DataTable MyDt = new DataTable(); 
  26. MyDa.Fill(MyDt);
  27.  
  28. GridView2.DataSource = MyDt;
  29.  
  30. GridView2.DataBind();
  31.  
  32. }
=====================

This is the code for delete WHICH DOES NOT WORK
Expand|Select|Wrap|Line Numbers
  1. protected void GridView2_RowDeleting(object sender,GridViewDeleteEventArgs e) 
  2.  
  3.  
  4. {
  5.  
  6.  
  7.  
  8. String S1;SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo"); 
  9. MyCon.Open();
  10.  
  11. Button b1 = new Button(); 
  12.  
  13.  
  14. foreach (GridViewRow gd in GridView2.Rows) 
  15. {
  16.  
  17. b1 = (Button)gd.FindControl("btDelete"); 
  18.  
  19. if (b1.CommandName=="Delete") 
  20. {
  21.  
  22. S1 = "delete from demo where id=" + gd.Cells[0].Text;
  23.  
  24. SqlCommand DeleteCmd = new SqlCommand(S1, MyCon); 
  25. DeleteCmd.ExecuteNonQuery();
  26.  
  27. Response.Write("deleted"); 
  28. }
  29.  
  30. }
  31.  
  32. }
  33.  
  34.  

This is the code on asp side
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" Style="z-index: 104; 
  2. left: 66px; position: absolute; top: 405px" Width="581px" OnRowEditing="GridView2_RowEditing" 
  3.  
  4. OnRowDeleting="GridView2_RowDeleting" OnRowUpdating="GridView2_RowUpdating" 
  5.  
  6.  
  7. OnSelectedIndexChanged="GridView2_SelectedIndexChanged">


ANY HELP PLEASE
Aug 2 '07 #1
2 2198
kenobewan
4,871 Expert 4TB
What error are you getting?
Aug 2 '07 #2
cmrhema
375 256MB
What error are you getting?
thanks for replying
i have sorted out the problem
it seems i missed out many points which i chalk it out.

1. In the asp side code i was supposed to add
<asp:GridView DataKeyNames="Id"
This i was not aware of.
2. this is the code i wrote in delete
Expand|Select|Wrap|Line Numbers
  1.  protected void GridView2_RowDeleting(object sender,GridViewDeleteEventArgs e)
  2.  
  3.         {
  4.  
  5.             int ID = (int)GridView2.DataKeys[e.RowIndex].Value;
  6.  
  7.             String S1;
  8.             SqlConnection MyCon = new SqlConnection("Server=HEMA\\SQLEXPRESS;Integrated Security=SSPI;database=demo");
  9.             MyCon.Open();
  10.             S1 = "delete from t1 where id=" + ID;
  11.             SqlCommand Deletecmd = new SqlCommand(S1, MyCon);
  12.             Deletecmd.ExecuteNonQuery();
  13.  
  14.         }
Now this works perfectly.
I must confess that i am very much embarassed to let you know the time taken for me to do this little piece of coding

Thanks
cmrhema
Aug 2 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by NateDawg | last post: by
1 post views Thread by JŁrgen Bayer | last post: by
1 post views Thread by JasonK | last post: by
3 posts views Thread by tarscher | last post: by
4 posts views Thread by Wannabe | last post: by
2 posts views Thread by William LaMartin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.