Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

GridView Delete Command

Posted on 2008-06-14
2
Medium Priority
?
543 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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…
Suggested Courses

722 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