Ich habe eine Gridview mit einer Checkbox drin und ich versuche, diesen Code zu tun, löschen Sie ein mehrere mit:So löschen Sie mehrere Zeilen in einer Gridview mit einem Kontrollkästchen?
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();
}
}
}
und hier ist der 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;
}
das funktioniert gut auf Löschen die nur ein Benutzer überprüft, aber wenn ich mehr als eine überprüfen bekomme ich diesen Fehler:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Ich setze den Gridview-Datakey auf die Spalte customer_id, die eine GUID enthält.
Ich benutze asp.net 4.0, was ist das Problem?
Ihnen sehr danken. – Wahtever