简体   繁体   English

索引超出范围异常

[英]Index out of range exception

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 ata 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. 我是stackoverflow的新手,所以对不起,这是太多或太少的信息。

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. 编辑:这是在C#ASP.NET中,我不确定错误在哪里,但是我相信它是在btnDeleteVideo_Click事件中。 I am showing the other event (btnAddVideo_Click) as reference if needed. 如果需要,我将显示另一个事件(btnAddVideo_Click)作为参考。 I can remove that if it would help. 如果有帮助,我可以删除它。

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. 我的猜测是您的问题出在btnDeleteVideo_Click方法中,因为您正在for循环中更改数据结构(GridView2.Rows),同时在for循环中引用它。 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. 我的建议是首先列出for循环中所有选中的行,然后一次删除多个行,而不是对每一行都进行删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM