Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7599061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:34:00+00:00 2026-05-30T22:34:00+00:00

I am working on a site that users can add and delete videos from

  • 0

I am working on a site that users can add and delete videos from a list.
All the adding and removing is done with checkboxes. I can add multiple videos at a time but when I try to delete more than one of them at a time from the list, it gives me this error:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

However when there are no issues removing one at a time. Also when I get the error and go back the checked videos are gone.
This is in C# ASP.NET and I am not sure where the error is, but I believe it to be in the btnDeleteVideo_Click event. I am showing the other event (btnAddVideo_Click) as reference if needed. I can remove that if it would help. I am new to stackoverflow so I am sorry if this is too much or too little info.

Here is the code for both the adding and removing events:

protected void btnAddVideo_Click(object sender, EventArgs e)
{

    foreach (GridViewRow gvr in GridView3.Rows)
    {
        CheckBox chkItem = (CheckBox)gvr.FindControl("cbAdd");
        if (chkItem.Checked)

        {
            String sRecID = GridView3.DataKeys[gvr.RowIndex].Value.ToString();
            Session["videorecid"] = sRecID;
            SqlDataSource2.Insert();
            SqlDataSource2.SelectCommand = "SELECT * FROM dealervideo inner join videos on videos.RecID = dealervideo.VideoRecID inner join dealers on dealers.RecID = dealervideo.DealerRecID where dealers.RecID = " + hidRecID.Value;
            GridView2.DataBind();
        }
    }
    GridView2.DataBind();
}

protected void btnDeleteVideo_Click(object sender, EventArgs e)
{

    foreach (GridViewRow gvr in GridView2.Rows)
    {
        CheckBox chkItem = (CheckBox)gvr.FindControl("cbDelete");
        if (chkItem.Checked)
        {
            String sRecID = GridView2.DataKeys[gvr.RowIndex].Value.ToString();
            Session["videorecid"] = sRecID;
            SqlDataSource2.Delete();
            SqlDataSource2.SelectCommand = "SELECT * FROM dealervideo inner join videos on videos.RecID = dealervideo.VideoRecID inner join dealers on dealers.RecID = dealervideo.DealerRecID where dealers.RecID = " + hidRecID.Value;
            GridView2.DataBind();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T22:34:01+00:00Added an answer on May 30, 2026 at 10:34 pm

    The problem is the logic of btnDeleteVideo_Click.

    Imagine you have 5 items in your list, numbered 0 to 4, and you try to delete 2 at once.

    Your code above now loops through all five rows. When it gets to the first deletion, it removes a row by deleting from the data source and rebinding.

    It now continues through the loop until it finds the second item marked from deletion – except that your grid now contains one less row since you deleted and rebound.

    So the line String sRecID = GridView2.DataKeys[gvr.RowIndex].Value.ToString(); will have a tendency to explode as the original RowIndex may now be higher than the actual number of rows.

    A better approach would be to work out all the rows you want to delete by loopin as you are, but only delete and rebind at the end.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a site that users can add and delete videos from
I'm working on a site where users can add points to other user, however
I'm working on a site that lets users add calendar entries, on the main
I am working on a Wordpress site that users can login to and make
I'm working on a site that can be displayed to the user in several
I'm working on a site that allows users to purchase digital content and have
On a site that I am working on, if users change the language settings
So I'm working on this site web app that should let users easily chat
I am working on a site similar to Craigslist where users can make postings
I realise that I can prevent unauthenticated users from accessing views at controller level

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.