Solved

GridView Delete Command

Posted on 2008-06-14
2
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

751 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