protected void Application_Start(object sender, EventArgs e)
        {
            try
            {
                ApplicationController objAppController = new ApplicationController();
                if (objAppController.IsInstalled())
                {
                    SageFrameConfig pagebase = new SageFrameConfig();
                    RolesManagementController objController = new RolesManagementController();
                    RolesManagementInfo res = objController.GetRoleIDByRoleName(SystemSetting.AnonymousUsername);
                    if (res != null)
                    {
                        SystemSetting.ANONYMOUS_ROLEID = res.RoleId.ToString();
                    }
                    bool IsUseFriendlyUrls = pagebase.GetSettingBollByKey(SageFrameSettingKeys.UseFriendlyUrls);
                    SageFrameSettingKeys.PageExtension = pagebase.GetSettingsByKey(SageFrameSettingKeys.SettingPageExtension);
                    bool isSchedulerOn = pagebase.GetSettingBollByKey(SageFrameSettingKeys.Scheduler);
                    if (IsUseFriendlyUrls)
                    {
                        RegisterRoutes(RouteTable.Routes);
                    }
                    if (isSchedulerOn)
                    {
                        RunSchedule();
                    }
                }
            }
            catch
            {
            }

        }
 protected void gdvUser_RowCommand(object sender, GridViewCommandEventArgs e)
 {
     try
     {
         if (!e.CommandName.Equals("Page"))
         {
             int rowIndex = int.Parse(e.CommandArgument.ToString());
             if (gdvUser.PageIndex > 0)
             {
                 rowIndex = int.Parse(e.CommandArgument.ToString()) - (gdvUser.PageSize * gdvUser.PageIndex);
             }
             hdnEditUsername.Value = gdvUser.DataKeys[rowIndex]["Username"].ToString();
             hdnEditUserID.Value = gdvUser.DataKeys[rowIndex]["UserId"].ToString();
             if (e.CommandName == "EditUser")
             {
                 string username = gdvUser.DataKeys[rowIndex]["Username"].ToString();
                 string[] userRoles = Roles.GetRolesForUser(username);
                 UserInfo sageUser = m.GetUserDetails(GetPortalID, hdnEditUsername.Value);
                 hdnCurrentEmail.Value = sageUser.Email;
                 txtManageEmail.Text = sageUser.Email;
                 txtManageFirstName.Text = sageUser.FirstName;
                 txtManageLastName.Text = sageUser.LastName;
                 txtManageUsername.Text = sageUser.UserName;
                 chkIsActive.Checked = sageUser.IsApproved == true ? true : false;
                 if (SystemSetting.SYSTEM_USERS.Contains(hdnEditUsername.Value) || hdnEditUsername.Value == GetUsername)
                 {
                     chkIsActive.Enabled = false;
                     chkIsActive.Attributes.Add("class", "disabledClass");
                     tabUserRoles.Visible = false;
                 }
                 else
                 {
                     tabUserRoles.Visible = true;
                 }
                 txtCreatedDate.Text = sageUser.CreatedDate.ToShortDateString();
                 txtLastActivity.Text = sageUser.LastActivityDate.ToShortDateString();
                 txtLastLoginDate.Text = sageUser.LastLoginDate.ToShortDateString();
                 txtLastPasswordChanged.Text = sageUser.LastPasswordChangeDate.ToShortDateString();
                 if (!sageUser.IsApproved)
                 {
                     txtLastActivity.Text = "N/A";
                     txtLastLoginDate.Text = "N/A";
                     txtLastPasswordChanged.Text = "N/A";
                 }
                 lstSelectedRoles.Items.Clear();
                 lstUnselectedRoles.Items.Clear();
                 RolesManagementController objController = new RolesManagementController();
                 List<RolesManagementInfo> objRoles = objController.PortalRoleList(GetPortalID, false, GetUsername);
                 foreach (RolesManagementInfo role in objRoles)
                 {
                     string roleName = role.RoleName;
                     if (SystemSetting.SYSTEM_ROLES.Contains(roleName, StringComparer.OrdinalIgnoreCase))
                     {
                         if (userRoles.Contains(roleName, StringComparer.OrdinalIgnoreCase))
                         {
                             lstSelectedRoles.Items.Add(new ListItem(roleName, roleName));
                         }
                         else
                         {
                             lstUnselectedRoles.Items.Add(new ListItem(roleName, roleName));
                         }
                     }
                     else
                     {
                         if (userRoles.Contains(roleName, StringComparer.OrdinalIgnoreCase))
                         {
                             string rolePrefix = GetPortalSEOName + "_";
                             roleName = roleName.Replace(rolePrefix, "");
                             lstSelectedRoles.Items.Add(new ListItem(roleName, roleName));
                         }
                         else
                         {
                             string rolePrefix = GetPortalSEOName + "_";
                             roleName = roleName.Replace(rolePrefix, "");
                             lstUnselectedRoles.Items.Add(new ListItem(roleName, roleName));
                         }
                     }
                 }
                 if (userRoles.Contains("Super User") && username.ToLower() == "superuser")
                 {
                     btnAddAllRole.Enabled = false;
                     btnAddRole.Enabled = false;
                     btnRemoveAllRole.Enabled = false;
                     btnRemoveRole.Enabled = false;
                     lstUnselectedRoles.Enabled = false;
                     lstSelectedRoles.Enabled = false;
                 }
                 PanelVisibility(false, false, true, false, false);
                 LoadUserProfileData();
             }
             else if (e.CommandName == "DeleteUser")
             {
                 if (hdnEditUsername.Value != "")
                 {
                     UserInfo user = new UserInfo(hdnEditUsername.Value, GetPortalID, Membership.ApplicationName, GetUsername);
                     m.DeleteUser(user);
                     ShowMessage("", GetSageMessage("UserManagement", "UserDeletedSuccessfully"), "", SageMessageType.Success);
                     BindUsers(string.Empty);
                 }
                 else
                 {
                     ShowMessage("", GetSageMessage("UserManagement", "SelectDeleteButtonOnceAgain"), "", SageMessageType.Alert);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         ProcessException(ex);
     }
 }
 private DataTable GetAllRoles()
 {
     DataTable dtRole = new DataTable();
     dtRole.Columns.Add("RoleID");
     dtRole.Columns.Add("RoleName");
     dtRole.AcceptChanges();
     RolesManagementController objController = new RolesManagementController();
     List<RolesManagementInfo> objRoles = objController.PortalRoleList(GetPortalID, false, GetUsername);
     foreach (RolesManagementInfo role in objRoles)
     {
         string roleName = role.RoleName;
         if (SystemSetting.SYSTEM_ROLES.Contains(roleName, StringComparer.OrdinalIgnoreCase))
         {
             DataRow dr = dtRole.NewRow();
             dr["RoleID"] = role.RoleId;
             dr["RoleName"] = roleName;
             dtRole.Rows.Add(dr);
         }
         else
         {
             string rolePrefix = GetPortalSEOName + "_";
             roleName = roleName.Replace(rolePrefix, "");
             DataRow dr = dtRole.NewRow();
             dr["RoleID"] = role.RoleId;
             dr["RoleName"] = roleName;
             dtRole.Rows.Add(dr);
         }
     }
     return dtRole;
 }
        protected void btnUserImport_Click(object sender, EventArgs e)
        {
            try
            {
                bool Flag = false;
                DataSet userImportDataSet = ImportUserFile();
                List<string> lstColumnHeader = new List<string>();
                List<string> ObjMappingList = MappingList();

                if (userImportDataSet.Tables.Count > 0)
                {
                    int columnLength = userImportDataSet.Tables[0].Columns.Count;
                    for (int i = 0; i < columnLength; i++)
                    {
                        lstColumnHeader.Add(userImportDataSet.Tables[0].Columns[i].ColumnName);
                    }

                    Flag = !ObjMappingList.Except(lstColumnHeader).Any();
                    if (Flag)
                    {
                        for (int i = 0; i < columnLength; i++)
                        {
                            if (txtImportUserName.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "UserName";
                            }
                            if (txtImportFirstName.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "FirstName";
                            }
                            if (txtImportLastName.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "LastName";
                            }
                            if (txtImportEmail.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "Email";
                            }
                            if (txtImportPassword.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "Password";
                            }
                            if (txtImportPasswordSalt.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "PasswordSalt";
                            }
                            if (txtImportPasswordFormat.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "PasswordFormat";
                            }
                            if (txtImportRoleName.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "RoleName";
                            }
                            if (txtImportPortalID.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "PortalID";
                            }
                            if (txtImportIsApproved.Text == userImportDataSet.Tables[0].Columns[i].ColumnName)
                            {
                                userImportDataSet.Tables[0].Columns[i].ColumnName = "IsActive";
                            }
                        }
                    }
                    else
                    {
                        ShowMessage("", GetSageMessage("UserManagement", "ColumnMappingError"), "", SageMessageType.Alert);
                        clearField();
                        return;
                    }

                    //Listing Excel Users
                    foreach (DataRow dr in userImportDataSet.Tables[0].Rows)
                    {
                        ExportUserInfo userImportInfo = new ExportUserInfo();
                        userImportInfo.UserName = dr["UserName"].ToString();
                        userImportInfo.FirstName = dr["FirstName"].ToString();
                        userImportInfo.LastName = dr["LastName"].ToString();
                        userImportInfo.Email = dr["Email"].ToString();
                        userImportInfo.Password = dr["Password"].ToString();
                        userImportInfo.PasswordSalt = dr["PasswordSalt"].ToString();
                        userImportInfo.PasswordFormat = dr["PasswordFormat"].ToString();
                        userImportInfo.RoleName = dr["RoleName"].ToString();
                        userImportInfo.PortalID = Convert.ToInt32(dr["PortalID"]);
                        userImportInfo.IsApproved = Convert.ToBoolean(dr["IsActive"]);
                        lstUserImportUsers.Add(userImportInfo);
                    }

                    //Extracting Excel Roles

                    List<RoleInfo> lstExcelRolesSplit = new List<RoleInfo>();
                    foreach (ExportUserInfo objExport in lstUserImportUsers)
                    {
                        string[] excelRolesArr = objExport.RoleName.Split(',');
                        foreach (string role in excelRolesArr)
                        {
                            RoleInfo objRoles = new RoleInfo();
                            objRoles.PortalID = objExport.PortalID;
                            objRoles.RoleName = role.Trim();
                            lstExcelRolesSplit.Add(objRoles);
                        }
                    }

                    List<RoleInfo> lstExcelRolesIdentical = new List<RoleInfo>();
                    var ExcelRolesIdentical = lstExcelRolesSplit.Select(i => new { i.RoleName, i.PortalID }).Distinct();
                    foreach (var objRole in ExcelRolesIdentical)
                    {
                        RoleInfo objRoleInfo = new RoleInfo();
                        objRoleInfo.RoleName = objRole.RoleName;
                        objRoleInfo.PortalID = objRole.PortalID;
                        lstExcelRolesIdentical.Add(objRoleInfo);
                    }

                    //Extracting Sage Roles
                    List<RolesManagementInfo> lstSageRoles = new List<RolesManagementInfo>();
                    RolesManagementController objController = new RolesManagementController();
                    lstSageRoles = objController.GetSageFramePortalList();
                    List<RoleInfo> lstSageRolesSplit = new List<RoleInfo>();
                    foreach (RolesManagementInfo objRoleMgntInfo in lstSageRoles)
                    {
                        RoleInfo objSageRoles = new RoleInfo();
                        objSageRoles.RoleName = objRoleMgntInfo.RoleName;
                        objSageRoles.PortalID = objRoleMgntInfo.PortalID;
                        lstSageRolesSplit.Add(objSageRoles);
                    }

                    //Retrieve Identical Roles in Sage Roles and Excel Roles
                    List<RoleInfo> lstIdenticalRoles = lstExcelRolesIdentical.Except(lstSageRolesSplit).ToList();

                    //Adding Identical Roles in SageRoles
                    for (int i = 0; i < lstIdenticalRoles.Count; i++)
                    {
                        RoleInfo objRole = new RoleInfo();
                        string rolePrefix = GetPortalSEOName + "_";
                        objRole.ApplicationName = Membership.ApplicationName;
                        objRole.RoleName = lstIdenticalRoles[i].RoleName;
                        objRole.PortalID = lstIdenticalRoles[i].PortalID;
                        objRole.IsActive = 1;
                        objRole.AddedOn = DateTime.Now;
                        objRole.AddedBy = GetUsername;
                        RoleController objRoleCon = new RoleController();
                        RoleCreationStatus status = new RoleCreationStatus();
                        objRoleCon.CreateRole(objRole, out status);
                    }

                    //Listing SageFrame Users
                    UserProfileController objUserProfile = new UserProfileController();
                    List<ExportUserInfo> lstSageUsers = objUserProfile.GetSageFrameUserList();

                    //Extracting Excel Username
                    List<string> lstExcelUserName = new List<string>();
                    lstExcelUserName = lstUserImportUsers.Select(x => x.UserName).ToList();

                    //Extracting Excel Email
                    List<string> lstExcelEmail = new List<string>();
                    lstExcelEmail = lstUserImportUsers.Select(x => x.Email).ToList();

                    //Extracting SageFrame Username
                    List<string> lstSageUserName = new List<string>();
                    lstSageUserName = lstSageUsers.Select(x => x.UserName).ToList();

                    //Extracting SageFrame Email
                    List<string> lstSageEmail = new List<string>();
                    lstSageEmail = lstSageUsers.Select(x => x.Email).ToList();

                    //Check duplicacy of Self Excel Users and Email
                    List<string> lstUserNameDuplicacyinExcel = new List<string>();
                    lstUserNameDuplicacyinExcel = lstExcelUserName.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();

                    List<string> lstEmailDuplicacyinExcel = new List<string>();
                    lstEmailDuplicacyinExcel = lstExcelEmail.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList();

                    if (lstUserNameDuplicacyinExcel.Count > 0 || lstEmailDuplicacyinExcel.Count > 0)
                    {
                        ShowMessage("", GetSageMessage("UserManagement", "DuplicateUsers"), "", SageMessageType.Alert);
                        clearField();
                        DeleteTempFolder();
                        return;
                    }

                    //check UserName duplicacy SageUsers And Excel Users               
                    List<string> lstUserNameDuplicacy = new List<string>();
                    lstUserNameDuplicacy = lstExcelUserName.Intersect(lstSageUserName).ToList();

                    //Removing duplicate List by UserName
                    ExportUserInfo dupUserListByUName = null;
                    List<ExportUserInfo> lstdubUserListByName = new List<ExportUserInfo>();
                    foreach (string DupUserName in lstUserNameDuplicacy)
                    {
                        List<ExportUserInfo> obj = lstUserImportUsers;
                        dupUserListByUName = lstUserImportUsers.Single(x => x.UserName == DupUserName);
                        lstUserImportUsers.Remove(dupUserListByUName);
                        //list users in excel
                        lstdubUserListByName.Add(dupUserListByUName);
                    }

                    //Extracting Email duplicacy in SageEmail and listUserImportUsers
                    List<string> lstExcelEmailInImportUsers = new List<string>();
                    lstExcelEmailInImportUsers = lstUserImportUsers.Select(x => x.Email).ToList();

                    //check Email duplicacy SageEmail And Excel Email 
                    List<string> lstEmailDuplicacy = new List<string>();
                    if (!m.RequireUniqueEmail)
                    {
                        lstEmailDuplicacy = lstExcelEmailInImportUsers.Intersect(lstSageEmail).ToList();
                    }
                    //Removing duplicate List by Email
                    ExportUserInfo dupUserListByEmail = null;
                    List<ExportUserInfo> lstdubUserListByEmail = new List<ExportUserInfo>();
                    if (lstUserImportUsers.Count != 0)
                    {
                        foreach (string DupEmail in lstEmailDuplicacy)
                        {
                            List<ExportUserInfo> obj = lstUserImportUsers;
                            dupUserListByEmail = lstUserImportUsers.Single(x => x.Email == DupEmail);
                            lstUserImportUsers.Remove(dupUserListByEmail);
                            //list users in excel
                            lstdubUserListByEmail.Add(dupUserListByEmail);
                        }
                    }

                    //Retrieve Duplicate UserList in SageUsers and Excel Users
                    lstDuplicateUserList = lstdubUserListByName.Concat(lstdubUserListByEmail).ToList();

                    //Retrieve Identical UserList in SageUsers and Excel Users
                    List<ExportUserInfo> lstIdenticalUserList = lstUserImportUsers;

                    //Adding Identical User List in SageUserList
                    if (lstIdenticalUserList.Count > 0)
                    {
                        for (int i = 0; i < lstIdenticalUserList.Count; i++)
                        {
                            UserInfo objUser = new UserInfo();
                            objUser.ApplicationName = Membership.ApplicationName;
                            objUser.FirstName = lstIdenticalUserList[i].FirstName;
                            objUser.UserName = lstIdenticalUserList[i].UserName;
                            objUser.LastName = lstIdenticalUserList[i].LastName;
                            objUser.Password = lstIdenticalUserList[i].Password;
                            objUser.PasswordSalt = lstIdenticalUserList[i].PasswordSalt;
                            objUser.Email = lstIdenticalUserList[i].Email;
                            objUser.SecurityQuestion = "";
                            objUser.SecurityAnswer = "";
                            objUser.IsApproved = lstIdenticalUserList[i].IsApproved;
                            objUser.CurrentTimeUtc = DateTime.Now;
                            objUser.CreatedDate = DateTime.Now;
                            objUser.UniqueEmail = 0;
                            objUser.PasswordFormat = Int32.Parse(lstIdenticalUserList[i].PasswordFormat);
                            objUser.PortalID = lstIdenticalUserList[i].PortalID;
                            objUser.AddedOn = DateTime.Now;
                            objUser.AddedBy = GetUsername;
                            objUser.UserID = Guid.NewGuid();
                            objUser.RoleNames = lstIdenticalUserList[i].RoleName;
                            objUser.StoreID = GetStoreID;
                            objUser.CustomerID = GetCustomerID;
                            UserCreationStatus status = new UserCreationStatus();
                            MembershipDataProvider.CreatePortalUser(objUser, out status, UserCreationMode.CREATE);
                        }
                    }
                    else
                    {
                        lblDuplicateUser.Visible = true;
                        ShowMessage("", GetSageMessage("UserManagement", "UsersNotAdded"), "", SageMessageType.Error);
                        ExportDuplicateUserList();
                    }
                    if (lstDuplicateUserList.Count > 0 && lstIdenticalUserList.Count > 0)
                    {
                        lblDuplicateUser.Visible = true;
                        ShowMessage("", GetSageMessage("UserManagement", "UsersAddedSuccessfullyWithDuplicateUserReport"), "", SageMessageType.Success);
                        ExportDuplicateUserList();
                    }
                    if (lstDuplicateUserList.Count == 0 && lstIdenticalUserList.Count > 0)
                    {
                        ShowMessage("", GetSageMessage("UserManagement", "UsersAddedSuccessfully"), "", SageMessageType.Success);
                    }
                    clearField();
                    DeleteTempFolder();

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
 private void BindRoles()
 {
     try
     {
         DataTable dtRoles = new DataTable();
         dtRoles.Columns.Add("Role");
         dtRoles.Columns.Add("RoleID");
         dtRoles.AcceptChanges();
         RolesManagementController objController = new RolesManagementController();
         List<RolesManagementInfo> objRoles = objController.PortalRoleList(GetPortalID, true, GetUsername);
         foreach (RolesManagementInfo role in objRoles)
         {
             string roleName = role.RoleName;
             if (SystemSetting.SYSTEM_ROLES.Contains(roleName, StringComparer.OrdinalIgnoreCase))
             {
                 DataRow dr = dtRoles.NewRow();
                 dr["Role"] = roleName;
                 dr["RoleID"] = role.RoleId;
                 dtRoles.Rows.Add(dr);
             }
             else
             {
                 string rolePrefix = GetPortalSEOName + "_";
                 roleName = roleName.Replace(rolePrefix, "");
                 DataRow dr = dtRoles.NewRow();
                 dr["Role"] = roleName;
                 dr["RoleID"] = role.RoleId;
                 dtRoles.Rows.Add(dr);
             }
         }
         gdvRoles.DataSource = dtRoles;
         gdvRoles.DataBind();
     }
     catch (Exception ex)
     {
         ProcessException(ex);
     }
 }