473,396 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2436
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

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

Similar topics

3
by: NateDawg | last post by:
I'm reposting this. I'm kinda in a bind untill i get this figured out, so if anyone has some input it would sure help me out. Ok, I’ve noticed a few gridview problems floating around the forum....
1
by: Jürgen Bayer | last post by:
Hi, I just tried out the ObjectDataSource of ASP.NET 2.0. A simple application works with a GridView bound to an ObjectDataSource. The ObjectDataSource is set to a (factory) class...
1
by: JasonK | last post by:
I would like to move the Delete button such that it displays one time in the footer row, rather than on every row. I've seen lots of questions asked on the subject around the net, but no answer...
3
by: tarscher | last post by:
Hi all, I have a grid that contains 7 columns from 3 tables (3 unique keys, 4 normal fields). I show this 7 columns on the gridview. I now want to add edit and delete functionality. This should...
0
by: Steve | last post by:
I have a gridview which uses an objectdatasource for its select and delete. The delete command uses the function below. The delete itself works but the extra logic which requires parameters...
4
by: Wannabe | last post by:
I am using ASP.Net 2.0 and have a gridview on my page. I have everything working except the delete command. The page reloads except the row I am trying to delete is still there. I believe it is...
3
by: jobs | last post by:
I've got a gridview that does not have a datasourceid assigned in the markup. I'd like to switch between two datasources in the codebehind. when I do switch, I first reset the the...
1
by: Barry L. Camp | last post by:
Hi all, Wondering if someone can help with a nagging problem I am having using a GridView and an ObjectDataSource. I have a simple situation where I am trying to delete a row from a table, but...
2
by: William LaMartin | last post by:
On webform, I am populating a GridView from a SQLDatasource based on a MySQL table named PIB. There is no vb code involved. Everything is done in the source for the aspx page, provided below. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.