Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

GridView Delete Command

Posted on 2008-06-14
2
Medium Priority
?
550 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 2000 total points
ID: 21786823
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
ID: 31467244
Thank you very much, that worked as needed.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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