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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:50:54+00:00 2026-05-31T20:50:54+00:00

I have a gridview with a checkbox in it and I’m trying to do

  • 0

I have a gridview with a checkbox in it and I’m trying to do a multiple delete using this code:

    protected void deleteUsers(object sender, EventArgs e) //deleting the selected users
{
    foreach (GridViewRow row in clientGrid.Rows) 
    {
        CheckBox selectBox = (CheckBox)row.FindControl("deleteUser");

        if (selectBox != null && selectBox.Checked) 
        {
            string bank, customerId, tMain, tSub;

            bank = bankName.InnerText;
            tMain = bank + "_main";
            tSub = bank + "_sub";

            customerId = Convert.ToString(clientGrid.DataKeys[row.RowIndex].Value);

            deleteSelected(tMain, tSub, customerId).ExecuteNonQuery();

            clientGrid.DataSource = getAllClients();
            clientGrid.DataBind();
        }
    }
}

and here is the sqlCommand:

    protected SqlCommand deleteSelected (string Tmain, string Tsub, string customerId) //the sql command for deleting
{
    string connection, commandSyntax;
    connection = ConfigurationManager.ConnectionStrings["localsqlserver"].ConnectionString;
    commandSyntax = "DELETE FROM [" + Tmain + "] FROM [" + Tsub + "] t1 " + 
                        "LEFT JOIN [" + Tmain + "] t2 ON t1.customer_id = t2.customer_id " +
                            "WHERE t1.customer_id = @customer_id" ;

    SqlConnection conn = new SqlConnection(connection);
    SqlCommand cmd = new SqlCommand(commandSyntax, conn);
    cmd.Parameters.AddWithValue("@customer_id", customerId);


    conn.Open();

    return cmd;
}

this works fine on deleting the only one checked user but if I check more than one I get this error:

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

I set the gridview datakey to the customer_id column which contains a guid.

I am using asp.net 4.0, what is the problem?

  • 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-31T20:50:56+00:00Added an answer on May 31, 2026 at 8:50 pm

    Your experiencing this because your rebinding your data grid inside of a foreach loop, causing the collection your currently iterating through to change in size, which in turn is throwing your index off.

    Instead try waiting until after you’ve performed all of your deletes to rebind your data grid.

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

Sidebar

Related Questions

I have gridview binded from database.. I have following code: protected void Page_Load(object sender,
Using Asp.net and jQuery I have a GridView with a CheckBox column. Right now
I have a gridview with checkbox inside asp:TemplateField . I wish to select multiple
I have drawn a checkbox in devexpress grid group row using the code below.
I have a gridview that I have added a checkbox column using a templatefield
I have a checkbox inside a gridview like this <asp:GridView ID=GridView1 runat=server BorderColor=Blue BorderWidth=1px
I have a gridview with Template field. I add a checkbox in templatefield .
I have a GridView what I fill through a LINQ expression. Something like this:
I have a GridView with a Checkbox on the first column: <asp:GridView ID=dgNumeradores runat=server
So I have a gridview with a checkbox field. In jquery, I'd like to

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.