私はそれのチェックボックスとGridViewのを持っていると私は、複数のこのコードを使用して削除やろうとしている:チェックボックスを使用してグリッドビュー内の複数の行を削除するにはどうすればよいですか?
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();
}
}
}
、ここでは、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;
}
これは削除の正常に動作し一つだけでは、ユーザーが、をチェックし、私は複数をチェックすると、私はこのエラーを取得する:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
グリッドビューのデータキーをguidを含むcustomer_idカラムに設定しました。
私はasp.net 4.0を使用していますが、何が問題ですか?
ありがとうございました。 – Wahtever