protected void BindUsersList()
        {
            try
            {
                var mlist = new PortalServiceManager().GetActivePortalUser();
                if (mlist == null)
                {
                    Session["_userList"]     = new List <portaluser>();
                    dgPortalUsers.DataSource = new List <portaluser>();
                    dgPortalUsers.DataBind();
                }

                Session["_userList"]     = mlist;
                dgPortalUsers.DataSource = mlist;
                dgPortalUsers.DataBind();
            }
            catch (Exception)
            {
                dgPortalUsers.DataSource = new List <portaluser>();
                dgPortalUsers.DataBind();
            }
        }
        protected void BindUsersList()
        {
            try
            {
                var mlist = new PortalServiceManager().GetLockedPortalUser();
                if (mlist == null)
                {
                    Session["_userList"]     = new List <portaluser>();
                    dgPortalUsers.DataSource = new List <portaluser>();
                    dgPortalUsers.DataBind();
                    ErrorDisplay1.ShowError("No Locked User Found");
                    return;
                }

                Session["_userList"]     = mlist;
                dgPortalUsers.DataSource = mlist;
                dgPortalUsers.DataBind();
            }
            catch (Exception)
            {
                dgPortalUsers.DataSource = new List <portaluser>();
                dgPortalUsers.DataBind();
            }
        }
        protected void DgPortalUsersDeleteCommand(Object source, DataGridCommandEventArgs e)
        {
            ErrorDisplay1.ClearError();
            //if (!Page.User.IsInRole("PortalAdmin"))
            //{
            //    ErrorDisplay1.ShowError("Sorry: You are not authorized to delete a user");
            //    return;
            //}
            try
            {
                dgPortalUsers.SelectedIndex = e.Item.ItemIndex;
                var portaluserId = (DataCheck.IsNumeric(dgPortalUsers.DataKeys[e.Item.ItemIndex].ToString())) ? long.Parse(dgPortalUsers.DataKeys[e.Item.ItemIndex].ToString()) : 0;
                if (portaluserId < 1)
                {
                    ErrorDisplay1.ShowError("Invalid Selection");
                    return;
                }

                int userId = int.Parse(((HiddenField)dgPortalUsers.SelectedItem.FindControl("hndUId")).Value.ToString(CultureInfo.InvariantCulture));
                if (userId < 1)
                {
                    ErrorDisplay1.ShowError("No User Item Selected");
                    return;
                }
                string userName = ((Label)dgPortalUsers.SelectedItem.FindControl("lblUserName")).Text;

                if (userName.Length < 2)
                {
                    ErrorDisplay1.ShowError("Invalid UserName!");
                    return;
                }

                var mUser = Membership.GetUser(userName, false);
                if (mUser == null)
                {
                    ErrorDisplay1.ShowError("Username not found!");
                    return;
                }
                //if (HttpContext.Current.User.Identity.Name.ToLower() == mName.ToLower())
                //{
                //    ErrorDisplay1.ShowError("You cannot delete the current user!");
                //    return;
                //}

                var ret = new PortalServiceManager().UpdateLockedUser(userId);

                if (!ret)
                {
                    ErrorDisplay1.ShowError("Unsuccessful Reset Operation!");
                    return;
                }


                string newPassword;
                try
                {
                    newPassword = mUser.ResetPassword();
                }
                catch (MembershipPasswordException ex)
                {
                    ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                    ErrorDisplay1.ShowError("Invalid password answer. Please re-enter and try again.");
                    return;
                }

                var body = string.Format("Password reset. Your new password is: {0}", Server.HtmlEncode(newPassword));

                if (Mailsender(mUser.Email, "ExpenseManager : User Credential Reset.", body))
                {
                    BindUsersList();
                    ErrorDisplay1.ShowSuccess("User Record has been Reset");
                }
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ErrorDisplay1.ShowError(ex.Message);
            }
        }
        private void SaveData()
        {
            try
            {
                var memUser = Membership.GetUser(txtUserName.Text.Trim());

                if (memUser != null)
                {
                    if (memUser.UserName.Length > 1)
                    {
                        ErrorDisplay2.ShowError("User Already Exist!");
                        mpeDisplayJobDetails.Show();
                        return;
                    }
                }

                var memUser2 = Membership.CreateUser(txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtEmail.Text.Trim());

                if (memUser2.UserName == "")
                {
                    ErrorDisplay2.ShowError("Process Failed! User Information was not registered");
                    mpeDisplayJobDetails.Show();
                    return;
                }

                memUser2.IsApproved = chkActive.Checked;
                Membership.UpdateUser(memUser2);
                var userId = new PortalServiceManager().GetUserIdByUsername(txtUserName.Text.Trim());
                if (userId < 1)
                {
                    Membership.DeleteUser(txtUserName.Text.Trim());
                    ErrorDisplay2.ShowError("Process Failed! Please try again soon");
                    mpeDisplayJobDetails.Show();
                    return;
                }

                var mUser = new portaluser
                {
                    DateRegistered = DateMap.GetLocalDate(),
                    Designation    = txtDesignation.Text.Trim(),
                    FirstName      = txtFirstName.Text.Trim(),
                    LastName       = txtLastName.Text.Trim(),
                    MobileNumber   = txtMobileNumber.Text.Trim(),
                    SexId          = int.Parse(ddlSex.SelectedValue),
                    TimeRegistered = DateTime.Now.ToString("hh:mm:ss"),
                    UserName       = txtUserName.Text.Trim(),
                    UserId         = userId,
                    Status         = chkActive.Checked
                };

                var k = (new PortalServiceManager()).AddPortalUser(mUser);

                if (k < 1)
                {
                    Membership.DeleteUser(txtUserName.Text.Trim());
                    ErrorDisplay2.ShowError("User Information Was Not Saved!");
                    mpeDisplayJobDetails.Show();
                    return;
                }

                //Add Roles
                string[] mRoles = Roles.GetRolesForUser(txtUserName.Text.Trim());

                if (mRoles != null)
                {
                    if (mRoles.Length > 0)
                    {
                        Roles.RemoveUserFromRoles(txtUserName.Text.Trim(), mRoles);
                    }
                }

                try
                {
                    foreach (ListItem item in chkRoles.Items)
                    {
                        if (item.Selected)
                        {
                            Roles.AddUserToRole(txtUserName.Text.Trim(), item.Value.Trim());
                        }
                    }
                }
                catch (Exception)
                {
                    Membership.DeleteUser(txtUserName.Text.Trim());
                    (new PortalServiceManager()).DeletePortalUser(k);
                    ErrorDisplay2.ShowError("Process Failed! Please try again soon");
                    return;
                }

                BindUsersList();

                ErrorDisplay2.ClearControls(tbUserInfo);
                ErrorDisplay1.ClearError();
                ErrorDisplay1.ShowSuccess("User Information Was Saved");
                mpeDisplayJobDetails.Hide();
            }
            catch (Exception ex)
            {
                ErrorManager.LogApplicationError(ex.StackTrace, ex.Source, ex.Message);
                ErrorDisplay2.ShowError(ex.Message);
                mpeDisplayJobDetails.Show();
            }
        }