Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Problems with Row_Deleting in GridView

Posted on 2009-07-14
Medium Priority
Last Modified: 2012-05-07

I'm having trouble deleting a row in GridView.  The error message states:  Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

The problem I think I'm having is finding the right code to return my datakeys.

My code is below.

Any help someone could provide would be great.


<asp:GridView ID="publications" runat="server"  
           Width="642px" onRowDeleting="publications_RowDeleting" DataKeyNames="subjectID, publicationID">
            <asp:BoundField DataField="publicationID" HeaderText="Publication ID"  ReadOnly="true" />
                <asp:TemplateField HeaderText="Subject Name">
                        <asp:TextBox ID="subjectName" runat="server" Text='<%# Bind("subjectName") %>'></asp:TextBox>
                        <asp:Label ID="subjectName" runat="server" Text='<%# Bind("subjectName") %>'></asp:Label>
                <asp:BoundField DataField="subjectID" ReadOnly="true"/>
                <asp:CommandField ShowDeleteButton="True" />
protected void publications_RowDeleting(Object sender, GridViewDeleteEventArgs e)
            string publicationID = publications.DataKeys[publications.SelectedIndex].Values["publicationID"].ToString();
            string subjectID = publications.DataKeys[publications.SelectedIndex].Values["subjectID"].ToString();
        SqlConnection conn;
        SqlCommand comm;
        string connectionString = ConfigurationManager.ConnectionStrings["publications"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("Delete subjects2pub where publicationID = @publicationID and subjectID = @subjectID ", conn);
        comm.Parameters.AddWithValue("@publicationID", publicationID);
        comm.Parameters.AddWithValue("@subjectID", subjectID);
            MessageBox.Text = "There was an error deleting this record.";
//partial code from BindGrid() to show that I believe I've set up the DataKeyNames correctly:
publications.DataKeyNames = new string[] { "publicationID" };
publications.DataKeyNames = new string[] { "subjectID" };

Open in new window

Question by:Finlay11
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
  • 2
LVL 41

Expert Comment

ID: 24851420
You are deleting the row and a=might be the Row is not selected i.e. SelectedIndex=-1
Use e.RowIndex like below:

string publicationID = publications.DataKeys[e.RowIndex].Values["publicationID"].ToString();
string subjectID = publications.DataKeys[e.RowIndex].Values["subjectID"].ToString();

Expert Comment

ID: 24851470
What's the DataSource for hte GridView?  If you configure your GridView to use a SqlDataSource or ObjectDataSource you get deleting more or less for free and it you generally don't have to worry about these head aches.  

If you're unfimiliar with SqlDataSources, here's a good tutorial:  http://www.asp.net/learn/data-access/tutorial-47-cs.aspx

Author Comment

ID: 24851471
Now I get the error:  Object reference not set to an instance of an object.
LVL 41

Accepted Solution

guru_sami earned 2000 total points
ID: 24851594
looks due to this:
publications.DataKeyNames = new string[] { "publicationID" };
publications.DataKeyNames = new string[] { "subjectID" };

Try this:
publications.DataKeyNames = new string[] { "publicationID" ,"subjectID" };

Author Closing Comment

ID: 31603323
That was it!  Thanks so much

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

610 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