You are not checking null for that operation. Check if Res.ElementAt(2) exists or not.
Try this,
CompId = Res.ElementAt(2) ?? 0;
if(CompId !=0)
{
Obj1.Sp_Insert_HeadHr_Login_Log(Tbx_Head_Hr_ID.Text.Trim(), CompId, Tbx_Pass.Text.Trim(), "Success", Ip);
}
This is not an accurate snippet. But you can test and modify accordingly.