Solved

GridView Delete Command

Posted on 2008-06-14
2
502 Views
Last Modified: 2010-04-21
Hello,

I'm having a problem deleting a row from a GridView however the row does in fact get removed from the GridView and also from the DB. This is where odd, and i have not been able to figure it out nor has a few other EE members. Please see the error message below.

Index was out of range. Must be non-negative and less than the size of the collection.
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: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 168:        WFID.SqlDbType = SqlDbType.Int
Line 169:        WFID.Direction = ParameterDirection.Input
Line 170:        WFID.Value = WWWFTPGridView.DataKeys(e.RowIndex.ToString).Item(0).ToString()
Line 171:
Line 172:        'Or you can even use this code below and not added the SQLParameter code for WFID.
0
Comment
Question by:asp_net2
2 Comments
 
LVL 14

Accepted Solution

by:
Dustin Hopkins earned 500 total points
Comment Utility
Ok, after reviewing the code from the 1st instance of this question.
You are calling the delete handler twice, and since you are rebinding manually after each delete, the row index of the original datakey no longer exists when the handler gets called the second time.

When setting up an event handler you have two choices in asp.net(vb). Either from the control's declaration, or from the handles clause in the sub declaration

in your case you need to remove one or the other...

'notice how you call the onrowdeleting event here

<asp:GridView ID="WWWFTPGridView" CssClass="WWWFTPGrid" runat="server" 

        AutoGenerateColumns="False" 

        AllowPaging="True" PageSize="2" 

        EmptyDataText="This server does not have any WWW/FTP services installed." 

        OnPageIndexChanging="WWWFTPGridView_PageIndexChanging" CellPadding="4" OnRowDeleting="WWWFTPGridView_RowDeleting" <---HERE 

        GridLines="None" ForeColor="#333333" DataKeyNames="wfid">
 

'and you also set it up here...please choose one or the other, not both

Protected Sub WWWFTPGridView_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles WWWFTPGridView.RowDeleting '<---HERE

Open in new window

0
 
LVL 4

Author Closing Comment

by:asp_net2
Comment Utility
Thank you very much, that worked as needed.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now