473,785 Members | 2,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete in GridView does not work

375 Contributor
hello,



i am new to asp.net. i am trying to do a simple application of adding,insertin g 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(properti es-columns) edit,update,can cel 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 2454
kenobewan
4,871 Recognized Expert Specialist
What error are you getting?
Aug 2 '07 #2
cmrhema
375 Contributor
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="I d"
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
13754
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. Everyone wants to do a java confirmation box when a user clicks the delete button. Fair enough, basic user design rules state that you should always confirm a delete action. There is also a consensus that the best way to do this is a template...
1
1935
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 (PersonFactory) with static methods (Select, Update, Delete, see below). The factory methods work with instances of a simple class (Person, see below). Select and Update works, but the Person object passed to the Delete method is always empty (Id == 0;...
1
6391
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 that seems to solve the problem. I have tried a few things: - adding a button, setting it's command name to Delete. Problem with this is that the CommandArgument value does not get set to the SelectedIndex of the GridView. Instead it has the...
3
2771
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 be fairly easy cos it's a build in feature of the gridview but alas... When the user clicks delete only one row on one table needs to be deleted thus I made a new query in my Datatable (DeleteissueQuery) that excpects an id
0
2667
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 AccountDebitID and AccountCreditID does not work. 'Delete a transaction Sub deletetrans(ByVal transactionid As Integer, ByVal transactionamount
4
2605
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 something really easy, but I cannot see it. The stored procedue works when run in QA. Can someone tell me what I am doing wrong/missing that is keeping the delete command from working in the gridview? Thank you. I am trying to delete a row out of...
3
7245
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 gridviewx.datasource = nothing and gridviewx.datasourceid = nothing before setting it to gridviewx.datasource = newdatasoruceid and then rebinding. All appears to work well until I try to excute a delete command that
1
8581
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 it doesn't seem to work at all. Below is my ASP.NET page, and further below, my VB.NET method that I am trying to call: <div id="admin-faq" class="page"> <h2>Site FAQs (Frequently Asked Questions)</h2>
2
2460
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. The update works fine, but a delete of a row produces the following error: Exception Details: System.Data.Odbc.OdbcException: ERROR SQLBindParameter not used for all parameters
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8976
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.