Solved

Problems with Row_Deleting in GridView

Posted on 2009-07-14
5
236 Views
Last Modified: 2012-05-07
Hi,

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.

Thanks.

<asp:GridView ID="publications" runat="server"  
            AutoGenerateColumns="False"
           Width="642px" onRowDeleting="publications_RowDeleting" DataKeyNames="subjectID, publicationID">
            <Columns>
            <asp:BoundField DataField="publicationID" HeaderText="Publication ID"  ReadOnly="true" />
                <asp:TemplateField HeaderText="Subject Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="subjectName" runat="server" Text='<%# Bind("subjectName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="subjectName" runat="server" Text='<%# Bind("subjectName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="subjectID" ReadOnly="true"/>
                <asp:CommandField ShowDeleteButton="True" />
            
            </Columns>
        </asp:GridView>
 
 
 
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);
        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
            BindGrid();
        }
 
        catch
        {
            MessageBox.Text = "There was an error deleting this record.";
        }
         finally
        {
            conn.Close();
        }
 
    }
 
//partial code from BindGrid() to show that I believe I've set up the DataKeyNames correctly:
BindGrid()
publications.DataKeyNames = new string[] { "publicationID" };
publications.DataKeyNames = new string[] { "subjectID" };

Open in new window

0
Comment
Question by:Finlay11
[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
  • 2
5 Comments
 
LVL 41

Expert Comment

by:guru_sami
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();
 
0
 
LVL 8

Expert Comment

by:ppittle
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
0
 

Author Comment

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

Accepted Solution

by:
guru_sami earned 500 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" };
0
 

Author Closing Comment

by:Finlay11
ID: 31603323
That was it!  Thanks so much
0

Featured Post

Independent Software Vendors: 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

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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