hi

i want to delete multiple report in grid view and i am facing some problem please help me as i am new to Asp.net.

 protected void LinkButton1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("chkSelect");
            if (cb.Checked)
            {
                int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                SqlDataSource1.DeleteParameters["id"].DefaultValue = rid.ToString();
                SqlDataSource1.Delete();
            }
            }
    }

Dani AI

Generated

Short diagnosis: that exception usually means the GridView.DataKeys collection doesn't contain an entry for the index you're asking for, or the Rows/DataKeys collections changed while you were iterating (for example because you called a delete that re-bound the GridView inside the loop). is right to point at the DataKeyNames configuration — if DataKeyNames isn't set (or doesn't match a field returned by your query) DataKeys will be empty. , also watch for null check and row type (header/footer rows can sneak in).

Practical, safe pattern (avoid modifying the GridView while iterating): first collect the selected primary keys, then perform deletes in one step. Example:

var ids = new List<int>();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
    var row = GridView1.Rows[i];
    if (row.RowType != DataControlRowType.DataRow) continue;
    var cb = row.FindControl("chkSelect") as CheckBox;
    if (cb != null && cb.Checked)
        ids.Add(Convert.ToInt32(GridView1.DataKeys[i].Value));
}

if (ids.Count > 0)
{
    // delete in one command (validate/parameterize in real code)
    using (var conn = new SqlConnection(connString))
    using (var cmd = conn.CreateCommand())
    {
        cmd.CommandText = "DELETE FROM YourTable WHERE ID IN (" + string.Join(",", ids) + ")";
        conn.Open();
        cmd.ExecuteNonQuery();
    }
}

Notes and troubleshooting:

  • Ensure GridView.DataKeyNames is set to the correct primary-key field returned by your Select (this populates DataKeys).
  • If you must delete inside the loop, iterate backwards (i = Rows.Count-1; i >= 0; i--) so removing a row doesn't change indices you have yet to visit.
  • Avoid calling SqlDataSource.Delete() inside a foreach over GridView.Rows — that call commonly triggers a rebind and will change Rows/DataKeys mid-iteration.
  • Add null checks for the checkbox and verify RowType == DataRow to avoid unexpected rows.

Just make sure that "DataKeyNames" property is set with any filed id(i.e EmployeeID, CustomerID etc). When you have set DataKeyNames property, Gridview control automatically create DataKey object for each row.

Here, It may be possible that you haven't set DataKeyNames and you are trying to retrieve the DataKey for a particualr row.

int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

So if not set then Set it with Gridview tag in design.

Let us know if it will not solve your problem.

hi

i want to delete multiple report in grid view and i am facing some problem please help me as i am new to Asp.net.

protected void LinkButton1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkSelect");
if (cb.Checked)
{
int rid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
SqlDataSource1.DeleteParameters["id"].DefaultValue = rid.ToString();
SqlDataSource1.Delete();
}
}
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.