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 7604837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:00:46+00:00 2026-05-31T00:00:46+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 a t 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.

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();
        }
    }
}

I am new to stackoverflow so I am sorry if this is too much or too little info.

EDIT:
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.

  • 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-31T00:00:47+00:00Added an answer on May 31, 2026 at 12:00 am

    My guess is that your problem is happening in the btnDeleteVideo_Click method because you are changing a data structure (GridView2.Rows) inside a for loop while referencing it in the for loop. This in most languages is bad practice. My advice is first make a list of all the checked rows in your for loop and then do a single delete of multiple rows in one shot rather than doing a delete for each row.

    • 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.