Set Item level permission in sharepoint through coding

Here is the way we can add item level permission for a user on an item,
SPRoleDefinition oUserRole;
SPRoleAssignment oUserRoleAssignment;
SPListItem itm;
SPSite site = null;
SPWeb web = null;
string siteUrl = Request.Url.ToString();

site = new SPSite(siteUrl);
web = site.OpenWeb();
SPGroup grp = web.Groups[groupname];

itm=web.Lists[“Shared Documents”].Items[0];

oUserRole = web.RoleDefinitions.GetByType(SPRoleType.Administrator);

oUserRoleAssignment = new SPRoleAssignment(loginName, emailId, userDisplayName, notes);//for user
or
oUserRoleAssignment = new SPRoleAssignment(grp);//for group

oUserRoleAssignment.RoleDefinitionBindings.Add(oUserRole);
itm.RoleAssignments.Add(oUserRoleAssignment);
web.AllowUnsafeUpdates = true;
itm.Update();
web.Update();
web.AllowUnsafeUpdates = false;

This is useful for setting item level permission via code.