Solved

GridView Delete Command

Posted on 2008-06-14
2
526 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
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

830 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