Delete a row in my table

Hi, I have this code in my asp file:

1    <asp:GridView ID="GridView" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="AccessDataSource1">
2        <FooterStyle BackColor="#CCCCCC" />
3        <Columns>
4            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
5                SortExpression="ID" />
6            <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
7            <asp:BoundField DataField="Nome" HeaderText="Name" SortExpression="Name" />
8            <asp:BoundField DataField="Apelido" HeaderText="Last Name" SortExpression="Last Name" />
9            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
10           <asp:ButtonField ButtonType="Button" CausesValidation="True" CommandName="Delete"
11               HeaderText="Apagar" ShowHeader="True" Text="Apagar" />
12       </Columns>
13       <RowStyle Font-Names="verdana" Font-Size="10px" />
14               <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
15               <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
16               <HeaderStyle BackColor="Black" Font-Bold="True" Font-Names="verdana" Font-Overline="False"
17                   Font-Size="12px" ForeColor="White" />
18               <AlternatingRowStyle BackColor="#CCCCCC" />
19   </asp:GridView>
As you can see in line 10, I want a button that delete one row from the table but I don't know how to do.
Can anyone explain to me? I know that I need to create one function in vb file but I don't know how.
9/20/2008 3:34:51 PM 72751 articles. 1 followers. Follow

11 Replies

Similar Articles

[PageSpeed] 26

After do what is saying in the link I get the following error:

Index was out of range. Must be non-negative and less than the size of the collection.

Why this happen?

9/20/2008 6:07:46 PM

u can use link button ... like this ...

1    <asp:GridView
2            id="Mygrd"
3            DataSourceId="srcGrid"
4            DataKeyNames="MyId"
5            AllowPaging="True"
6            AutoGenerateColumns="False"
7            OnRowCreated="Mygrd_RowCreated" 
8            .......>
9            <Columns>
10           <asp:CommandField ShowSelectButton="true" 
11               SelectText="Details" ShowDeleteButton="true"   />
13           <asp:BoundField DataField="First" HeaderText="First Field" />
.......... . . . . .
1    <asp:SqlDataSource
2            id="srcGrid"
3            ConnectionString="...ur connection string...."
5            SelectCommand="... ur select command .."
6            DeleteCommand="DELETE mytable WHERE MyId=@MyId"
7            Runat="server">
8            <DeleteParameters>
9           <asp:ControlParameter  
10           Name="MyId" 
11           ControlID="Mygrd" 
12           Direction="Input"  Type="...."
13           PropertyName="SelectedValue" />
14            </DeleteParameters>
15           </asp:SqlDataSource>

here is code for Mygrd_RowCreated... u can insert this into code behind file or between <script> and </script> tags in ur aspx file....

Protected Sub Mygrd_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lnkDelete As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
"onclick") = "return confirm('Delete Record?')"
End If
End Sub

Marks as answer if that helps

Learn by Sharing
9/20/2008 6:20:20 PM


Appears this error:

Specified argument was out of the range of valid values.
Parameter name: index

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:

Line 5:      Protected Sub Mygrd_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Line 6:          If e.Row.RowType = DataControlRowType.DataRow Then
Line 7:              Dim lnkDelete As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)
Line 8:              lnkDelete.Attributes("onclick") = "return confirm('Delete Record?')"
Line 9:          End If

9/20/2008 8:46:36 PM

I think the problem is with index of gridview, because it try to show more row that my table have. Am I Right?

How can I fix this?

9/20/2008 11:05:57 PM

can u put some grid code here .. i think u r misplacing the link button u need to change the index accordingly...

Learn by Sharing
9/21/2008 8:20:35 AM

Dim lnkDelete As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton)

try using findcontrol() method, 

Dim lnkDelete As LinkButton = CType(e.Row.FindControl("herelinkbuttonid"), LinkButton)

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
9/21/2008 8:26:52 AM


I fixed the problem changing this line: Dim lnkDelete As LinkButton = CType(e.Row.Cells(0).Controls(0), LinkButton) to Dim lnkDelete As LinkButton = CType(e.Row.Cells(3).Controls(0), LinkButton) .

The problem was that I've the delete button in column 3.


Many thanks.

9/22/2008 8:34:37 AM

if my code helped u .. then u shld hve marked my post as answer

Learn by Sharing
9/22/2008 11:50:09 AM


It's done.

9/22/2008 12:48:53 PM

well you have marked you own  and ramiya's post as answer... Smile

Learn by Sharing
9/22/2008 5:16:44 PM

Similar Artilces:

adding / deleting table rows, and accessing controls within added rows
Hi everyone,I would like to know what is a good and simple way to achieve this using  Basically i would like the ability to add and delete rows, preferably table rows.  Against each row is its delete button, hitting it will delete the corresponding row.  There is only 1 Add button to add a new row.  In each row, there are say 2 textboxes.  When i hit, say a calculate button, i wish to be able to access all the available textboxes in all the available rows in the event handler, and say just output a sum.If GridView is suggested, could you please explain why i...

Deleting rows from one table based upon rows in another table
I need to delete rows from table1 based on rows in table2. table1 has 1.5 million rows from which I need to delete 20,000 existing in table2. The table structures are the same and I must exactly match 10 (of the 15) columns in the tables. I also need to limit the number of rows being worked on, in a single transaction, to 500 or so. Hi Mike, try the following: set rowcount 500 DELETE table1 from table1 t1,table2 t2 where t1.col1=t2.col1 and t1.col2=t2.col2 and t1.col3=t2.col3 and ........ t1.col10=t2.col10 HTH. Regards P:S You must re-execute the DML statement above 40 time...

Delete row and delete from table
Hi I need, after delete a grid row, execute a delete statmentProtected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand Dim advogado As StringIf (e.CommandName = "Apagar") Thenadvogado = RTrim(e.CommandArgument.ToString)Apaga_Processos(advogado)End Ifend subPrivate Sub Apaga_Processos(ByVal advogado As String)Dim conn As SqlConnectionDim comm As SqlCommandDim connectionString As String = ConfigurationManager.ConnectionStrings("").ConnectionStringconn = New Sql...

Is having a trigger that inserts a row in Table 'A', when a row in same table is inserted by ADo.Net code?
I want to insert a row for a Global user  in Table 'A' whenever ADO.Net code inserts a Local user row into same table. I recommended using a trigger to implement this functionality, but the DBA was against it, saying that stored proecedures should be used, since triggers are unreliable and slow down the system by placing unecessary locks on the table. Is this true OR the DBA is saying something wrong? My thinking is that Microsoft will never include triggers if they are unreliable and the DBA is just wanting to offload the extra DBA task of triggers to the programmer so that a s...

Showing table columns from multiple tables in Formview, but insert/update/delete rows of Master table
I have a page on which I have GridView and FomView control. On selection of a row in GridView the row gets displayed in the Item template of the FormVIew. The values are the result of inner join from multiple tables i.e. table Stock,Product and Workshop.  Please let me know how I could insert/update/delete data for the base table i.e. Stock table, but be able to see the column values from the associated tables on the Itemtemplate,Edittemplate and Inserttemplate of the FormView. Thanks,Anita Hi:   Take a look at this tutorial. It's for DetailsView and same ...

masterpage and control formatting (table control, HTML table control)
I have a master page that contains 1 contentplaceholder.  In my default.aspx (that uses the masterpage layout) I have a table server control.  During page_load I get the data I need and repeatedly add the usercontrol to the table control, one user control per row.  My user control is basically a HTML table with several rows and columns that's populated with data upon creation. I want this table to fill the contentplaceholder's width. In addition within the masterpage I have a CSS link for my stylesheet. Here are my questions: Is there a better way of adding ...

How to add a footer row to a table after deleting all footer rows
I'm sure this is a very easy thing to do, but I can't figure out how to do it. I have a table, and I deleted all the footer rows.  Now I would like to add one footer row.  How do I do that? If I right-click on the detail row and select Insert Row Below, it is, of course, another detail row. I'm stumped -- thanks for any help you can give!Cynthia Do you have groups in your table?  You should be able to right click on any table row and toggle the corresponding menu item to show the table header, details or footer.  It is in the right click menu includ...

How to delete controls in GridView when the delete button in a row is clicked
Hi,   I am having issues with my gridview.....i added 2 dropdown and 2 compare validators....whenever i delete a row though......the dropdown and compare validators are becoming null........thus when i click a button, it is still validating.............anyway to delete this when i click the delete button or even handle?   Please help.  ...

Need to delete rows from table if checkboxes are checked for row
Hello. I have a linkbutton that will delete individual rows from my gridview and update the database, however, I want to be able to make multiple selections via the checkboxes which appear on each row, and click just one button to delete all the checked rows. The following is from my aspx page:  <div style="float: right;"> <asp:Button ID="btnLinkBack" runat="server" Text="Check Backlinks" /> <asp:Button ID="btnApprove" runat="server" Text="Unhide" /> ... problem: i can not view image data for the rest of my table rows except the image for the first row but i can read all the other non image fields for all rows.
Hi i have a table with the following fields imgdata........image imgtitle.........varchar(max) imgType........varchar(max) imgLength.....bigint i have no problem with uploading the image but viewing the uploaded image(s) in a gridview has caused me great pain. The probelm is i have two pages. page1 grabs the image to display and page2 has a gridview that displays the grabed image. I wanted to have an sql statement like SELECT * FROM mytable WHERE incident_id=someValue. IF i have 5 records in this table and i view page2 ...

How to add the data present in the controls to a table control without any database involvement.
Hi All,i am four controls where i will write the text and once i click the add button those should get added to the table .i want 6 columns fixed in the table and rows as many as required.i tried doing that taking a table server control and able to load the table.but the problem is whenver i am adding a new row the old one is gfetting overwritten by this and at the end i am getting only one row.  System.Web.UI.WebControls.Table t1 = new System.Web.UI.WebControls.Table();  i have declared the below one global TableRow tr; TableCell tc1; TableCell tc2; TableCell tc...

Deleting Child Table Rows and not from the master table using SqlDataAdapter.Update()
I have two tables tblPerson and tblPhone. tblPerson has: PersonID Name tblPhone has: PhoneID PersonID PhoneNumber I am using SqlDataAdapter.Update to delete the rows from the tables. I am also using Typed DataSets and the relation between the two tables depends upon the PersonID. When I delete from the tblPerson the corresponding record of that person is also deleted from the tblPhone which is good. When I try to delete from the tblPhone I dont want the Person record to be deleted and it throws exception:  Failed to enable constraints. One or more rows contain ...

Deleting rows from a table
I am using Thunderbird v 2 in Windows 2000. I send out a newsletter to a boating organization and include a table of upcoming rendezvous. For next month I want to remove the rendezvous for February. I want to remove the data but also the row. When I go into table configuration and reduce the number of rows it lops one off of the bottom. I don't see how to get rid of the empty row on top. Please advise. Thank you, The date and time was Thursday, February 12, 2009 9:44:23 PM, and on a whim, Gualtier Malde pounded out on the keyboard: > I am using Thunderbird v 2 ...

Deleting all the rows in a table
To: all Pro I know that there are two methods to delete all the row in remote db during synchronization. one is use download _delete_cursor, another is use SQL statement [STOP/START synchronizaton DELETE]. For the first method, when I use [select NULL, NULL], it shows "Error Context" , "ODBC: [Microsoft][ODBC SQL Server Driver]Restricted data type attribute violation (ODBC State = 07006, Native error code = 0, Column or parameter #2). Table Name: orderdo". For the second method, i am not sure where should i write the code. call store prodeure or any mobilink e...

How to delete row from GridView control
Hello,  I am working on an ASP.NET 2.0 app, and I am trying to wire up a "Delete" button to a gridview control on a form. Here is the code for the ASP :  <asp:GridView ID="gvInConditions" runat="server" AutoGenerateColumns="true" OnRowDeleting="gvInConditions_RowDeleting" DataKeyNames="Condition_ID" >         <Columns>                <asp:CommandField ButtonType="Button" SelectText=&...

Web resources about - Delete a row in my table -

Delete key - Wikipedia, the free encyclopedia
, known less ambiguously as forward delete ) discards the character ahead of the cursor 's position, moving all following characters one position ...

Facebook Fined $77K by Argentine Court for Failing to Delete Alleged Defamatory Groups
... mess in Argentina , as PanAm Post reported that a Rosario court ordered the social network to pay compensation of $77,000 for failing to delete ...

Facebook adds search history to Activity Log so users can delete queries
... in users’ Activity Logs. The feature will be rolled out to all users in the next few weeks. The feature allows for users to view and delete ...

Tweets MPs Delete (@deletedbyMPs) on Twitter
Sign in Sign up You are on Twitter Mobile because you are using an old version of Internet Explorer. Learn more here Tweets MPs Delete @ deletedbyMPs ...

Oren Jacob's answer to Pixar Animation Studios: Did Pixar accidentally delete Toy Story 2 during production ...
Oren Jacob's answer: Hi everyone, I'm the Oren Jacob in the video. Hopefully I can offer some first person color commentary about the video above ...

Tweeticide - Delete All of Your Twitter Tweets at Once! on the App Store on iTunes
Get Tweeticide - Delete All of Your Twitter Tweets at Once! on the App Store. See screenshots and ratings, and read customer reviews.

Delete, and I mean it! - Flickr - Photo Sharing!
I bought a new keyboard at work because I spilled cereal on the previous one and some of the keys stopped working. The new one has a giant delete ...

Facebook deletes Perth page dedicated to school fights
A page filled with videos of Perth school students fighting has been deleted by Facebook, as a video of people brawling in Perth CBD hit headlines. ...

Bank of America gets Twitter to delete journalist’s joke, says he violated copyright
The founding editor of Business Insider UK, Jim Edwards , had a bank delete two of his tweets today. In an e-mail, Bank of America told Edwards ...

Bank of America Merrill Lynch persuaded Twitter to delete my tweets
I had an annoying surprise this weekend: An email from Bank of America Merrill Lynch telling me that Twitter had deleted two of my tweets for ...

Resources last updated: 1/10/2016 5:20:47 AM