public static UserPermission FillUserPermission(IDataReader sq)
 {
     string output = sq.FieldCount.ToString();
     UserPermission perm = new UserPermission();
     perm.Control = sq.GetString(0);
     perm.AccessValue = sq.GetByte(1) == (byte)1 ? true : false;
     if (sq.FieldCount > 2)
     perm.Permission = sq.IsDBNull(2) ? "" : sq.GetString(2);
     return perm;
 }
        public static void DeleteControlPermissionAccess(int userID, UserPermission ucp)
        {
            DAL.DAL.ExecuteNonQuery("D_UserAccessPermission_ByUserID_ByControlName_ByPermissionName",
                                    DAL.DAL.Parameter("UserID", userID),
                                    DAL.DAL.Parameter("ControlName", ucp.Control),
                                    DAL.DAL.Parameter("PermissionName", ucp.Permission)

                                    );
        }
        private static void comparePermissions(int currentID, Dictionary<string, HashSet<string>> oldPermissions, Dictionary<string, HashSet<string>> newPermissions, List<Task> tasklist, bool AccessValue)
        {
            if (!oldPermissions.Equals(newPermissions))
            {
                UserControlPermission ucp = new UserControlPermission();
                UserPermission up = new UserPermission();
                HashSet<string> deletedControls = new HashSet<string>();
                //added permissions
                foreach(var delPerms in oldPermissions)
                {
                    if (!newPermissions.Keys.Contains(delPerms.Key))
                    {
                        deletedControls.Add(delPerms.Key);
                        tasklist.Add(Task.Factory.StartNew(() => DeleteControlAccess(currentID, delPerms.Key)));
                    }
                }
                foreach (var permission in newPermissions)
                {
                    if (!deletedControls.Contains(permission.Key) && oldPermissions.ContainsKey(permission.Key))
                    {
                        //delete all permissions from control
                        if (!(permission.Value.Count > 0))
                            tasklist.Add(Task.Factory.StartNew(() => DeleteControlPermissions(currentID, permission.Key)));

                        HashSet<string> DeletePermissions = new HashSet<string>();
                        HashSet<string> AddPermissions = oldPermissions[permission.Key];

                        if (AddPermissions != null)
                        {
                            DeletePermissions.AddRange(oldPermissions[permission.Key]);
                            foreach (string s in permission.Value)
                            {
                                if (!DeletePermissions.Remove(s))
                                {
                                    up.Control = permission.Key;
                                    up.Permission = s;
                                    up.AccessValue = AccessValue;
                                    //add permission
                                    tasklist.Add(Task.Factory.StartNew(() => SetPermissionByUserID(currentID, up)));
                                }
                            }
                        }
                        else { throw new Exception("addpermission = null"); }
                        if (DeletePermissions.Count > 0)
                        {
                            foreach (string s in DeletePermissions)
                            {
                                up.Control = permission.Key;
                                up.Permission = s;
                                tasklist.Add(Task.Factory.StartNew(() => DeleteControlPermissionAccess(currentID, up)));
                            }
                        }
                    }
                    else
                    {
                        ucp.Control = permission.Key;
                        ucp.AccessValue = true;
                        tasklist.Add(Task.Factory.StartNew(() => SetPermissionByUserID(currentID, ucp)));
                        
                        foreach (string s in permission.Value)
                        {
                            up.AccessValue = AccessValue;
                            up.Control = permission.Key;
                            up.Permission = s;
                            tasklist.Add(Task.Factory.StartNew(() => SetPermissionByUserID(currentID, ucp)));
                        }
                    }

                }
            }
        }
 //INSERT METHODS
 public static void SetPermissionByUserID(int userID, UserPermission ucp)
 {
     byte Value = (byte)(ucp.AccessValue ? 1 : 0);
     DAL.DAL.ExecuteNonQuery("I_User_ControlPermission_ByControlName_ByPermissionName",
         DAL.DAL.Parameter("UserID", userID), 
         DAL.DAL.Parameter("ControlName", ucp.Control), 
         DAL.DAL.Parameter("PermissionName", ucp.Permission),
         DAL.DAL.Parameter("Value", Value)
     );
 }
        public static void AddPermissionsFromList(int userid, Dictionary<string, HashSet<string>> perm, bool accessValue)
        {
            foreach (var z in perm)
            {
                if (z.Value == null || (z.Value != null && !(z.Value.Count > 0)))
                {
                    UserControlPermission ucp = new UserControlPermission();
                    ucp.AccessValue = accessValue;
                    ucp.Control = z.Key;
                    SetPermissionByUserID(userid, ucp);
                }

                if (z.Value != null && z.Value.Count > 0)
                {
                    foreach (string action in z.Value)
                    {
                        UserPermission up = new UserPermission();
                        up.AccessValue = accessValue;
                        up.Control = z.Key;
                        up.Permission = action;
                        SetPermissionByUserID(userid, up);
                    }
                }
            }
        }