This is my method of deleting from gridview:
protected void TabelaAlimentos_DeleteRow(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Delete")
{
GerenciarAlimentacaoController controller = new GerenciarAlimentacaoController();
int index = Int32.Parse((string)e.CommandArgument);
int id = Int32.Parse(TabelaAlimentos.Rows[index].Cells[0].Text);
Alimento alimento = (Alimento)controller.pesquisar(id, "Alimentos");
controller.deletar(alimento);
TabelaAlimentos.DeleteRow(index);
TabelaAlimentos.DataBind();
FileInfo file = new FileInfo(Server.MapPath("~/img/Alimentos/") + alimento.imagemAlimento);
file.Delete();
Response.Write("<script language='javascript'> alert('Alimento Removido com Sucesso!'); window.location=('gerenciaralimentacao.aspx'); </script>");
}
}
The code normally runs without error, but when it exits the method the error is generated.