Problems with Row_Deleting in GridView


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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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();
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:
Finlay11Author Commented:
Now I get the error:  Object reference not set to an instance of an object.
looks due to this:
publications.DataKeyNames = new string[] { "publicationID" };
publications.DataKeyNames = new string[] { "subjectID" };

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Finlay11Author Commented:
That was it!  Thanks so much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.