I can't tell you what is wrong, but I will say that I do not put functions inside my square brackets for this very purpose. Here is how I would debug this code. Put this in your "try" block.
int x = doc_item.IndexOf(p);
if (x >= 0 && x < res_bom.Count())
{
res_bom[x] = onbom;
}
else
{
MessageBox.Show("res_bom has " + res_bom.Count().ToString() +
" items and you are looking for number " + x.ToString();
}