/// <summary>
 /// Insert User
 /// </summary>
 /// <param name="userInfo">UserInfo object</param>
 /// <returns>Number of rows are effected</returns>
 public static int Insert(tblUser userInfo)
 {
     // Encode password & username
     userInfo.Password = Common.Encode(userInfo.Password);
     var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
     db.AddTotblUsers(userInfo);
     int re = db.SaveChanges();
     db.Connection.Close();
     return re;
 }
        /// <summary>
        /// Logins this instance.
        /// </summary>
        private void Login()
        {
            try
            {
                //logger.Info("btnLogin_Click");
                if (Validate())
                {                    
                    var objTblUer = new tblUser();
                    objTblUer.UserName = txtUsername.Text.Trim();
                    objTblUer.Password = txtPassword.Text.Trim();
                    var userInfoNew = UserFactory.GetUserInfo(objTblUer);
                    
                    if (userInfoNew != null && !userInfoNew.UserName.Equals(string.Empty)) // Login sucessfully
                    {
                        // Bind UserInfo
                        var userInfo = new UserInfo();
                        userInfo.UserName = userInfoNew.UserName;
                        userInfo.Name = userInfoNew.Name;
                        userInfo.Password = userInfoNew.Password;
                        userInfo.UserID = userInfoNew.UserID;

                        //get all User's permission
                        userInfo.UserPermission = UserGroupPermissionFactory.GetAllPermissionOfUser(userInfo.UserID);
                        //if user is admin, set all permission for admin
                        if (userInfo.UserName == "admin")
                        {
                            userInfo.UserPermission = UserGroupPermissionFactory.GetAllPermissionForAdmin();
                        }
                        // Redirect to the main form
                        var mainForm = new frmMainForm(userInfo);
                        mainForm.WindowState = FormWindowState.Maximized;
                        mainForm.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show(ConstantInfo.MESSAGE_LOGIN_FAIL, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                if (GlobalInfo.IsDebug) MessageBox.Show(ex.ToString());
            }
        }
 /// <summary>
 /// Gets the user info.
 /// </summary>
 /// <param name="userInfo">The user info.</param>
 /// <returns></returns>
 public static tblUser GetUserInfo(tblUser userInfo)
 {
     try
     {
         _db.Connection.Open();
         // Encode the password & username
         userInfo.Password = Common.Encode(userInfo.Password);
         return
             _db.tblUsers.Where(g => g.UserName.Equals(userInfo.UserName, StringComparison.OrdinalIgnoreCase) && g.Password.Equals(userInfo.Password) && g.IsActive == 1).
                 FirstOrDefault();
     }
     catch (Exception ex)
     {
         logger.Error(ex.ToString());
         throw;
     }
     finally
     {
         _db.Connection.Close();
     }
 }
 /// <summary>
 /// Bind data to controls
 /// </summary>
 private void BindControlToData(ref tblUser tblUser)
 {
   try
   {
     tblUser.UserName = txtUserName.Text.Trim();
     tblUser.Password = txtPassword.Text.Trim();
     tblUser.Email = txtEmail.Text;
     tblUser.Name = txtName.Text;
     tblUser.Address = txtAddress.Text;
     tblUser.PhoneNumber = txtPhone.Text;
     tblUser.IsActive = Convert.ToInt16(cbActive.Checked);
   }
   catch (Exception ex)
   {
     logger.Error(ex.ToString());
     if (GlobalInfo.IsDebug) MessageBox.Show(ex.ToString());
   }
 }
    private void btnAdd_Click(object sender, EventArgs e)
    {
      try
      {
        // Validate
        if (Validate())
        {
          // Check the existing username   
          if (!UserFactory.CheckIsUserExisting(txtUserName.Text.Trim()))
          {
            // Insert to database
            var userInfo = new tblUser();
            BindControlToData(ref userInfo);
            // Insert to database
             UserFactory.Insert(userInfo);
            _parrent.BindData(UserFactory.SelectAllUser());
            //TODO: Need to check number of rows are effected
            MessageBox.Show(ConstantInfo.MESSAGE_INSERT_SUCESSFULLY);

            //get userInfor after insert
            tblUser user = UserFactory.getUserByUserName(txtUserName.Text.Trim());
            _userID = user.UserID;

            //show tab listGroup and ListPermissior
            tabControl1.TabPages.Add(_tabGroup);
            tabControl1.TabPages.Add(_tabPermission);

            btnAdd.Enabled = false;
            EnableTab(_tabUser, false);
            btnClose.Enabled = true;
            //this.Close();
          }
          else
          {
            MessageBox.Show(ConstantInfo.MESSAGE_USERNAME_EXISTING);
          }
        }
      }
      catch (Exception ex)
      {
        logger.Error(ex.ToString());
        if (GlobalInfo.IsDebug) MessageBox.Show(ex.ToString());
      }
    }
        /// <summary>
        /// Update user
        /// </summary>
        /// <param name="userInfo">userInfo object</param>
        /// <returns>Number of rows are effected</returns>
        public static int Update(tblUser userInfo)
        {
            // Encode password & username
            userInfo.Password = Common.Encode(userInfo.Password);

            var db = new dbEcustomEntities(Common.Decrypt(ConfigurationManager.ConnectionStrings["dbEcustomEntities"].ConnectionString, true));
            var usrOrgin = db.tblUsers.Where(g => g.UserID == userInfo.UserID).FirstOrDefault();
            if (usrOrgin == null) return -1;
            db.Attach(usrOrgin);
            db.ApplyPropertyChanges("tblUsers", userInfo);
            int re = db.SaveChanges();
            db.Connection.Close();
            return re;
        }