I am following on a tutorial from here.
But I got jammed at the part where I need to grep the primary key of the checked rows.
Below is my current code:
protected void DeleteChecked_Click(object sender, EventArgs e)
{
bool atLeastOneRowDeleted = false;
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("UserSelector");
if (cb != null && cb.Checked)
{
atLeastOneRowDeleted = true;
int employeeID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
SqlDataSource1.SelectCommand = "DELETE FROM [UserDB] where Employee like " +employeeID;
SqlDataSource1.DataBind();
}
}
}
I do not know what I should change and how do I grep my ‘Employee’ variable on my GridView so that I can insert it into my DELETE statement.
Below is my sample GridView and the error I am meeting now.


Set a breakpoint on that line and look at the value of GridView1.DataKeys. I bet there are no keys in that array, so DataKeys[0] will throw that exception.