Example #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidationUtility.FieldNotAllowNull(this) == false)
            {
                lblNotify1.SetText(UI.hasnoinfomation, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            if (txtPassword.Text.Trim() != txtRetype.Text.Trim())
            {
                lblNotify1.SetText(UI.passwordnotsame, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            if (txtPassword.Text.Length < 5)
            {
                lblNotify1.SetText(UI.passwordtooshort, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            if (cbbUserGroup.SelectedValue == null || cbbDepartment.SelectedValue == null)
            {
                lblNotify1.SetText(UI.hasnoinfomation, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            if (!rbtnFemale.Checked && !rbtnMale.Checked)
            {
                return;
            }
            //Create User
            Users user = new Users();

            user.UserName    = txtUserName.Text;
            user.Password    = txtPassword.Text;
            user.FullName    = txtFullName.Text;
            user.Phone       = txtPhone.Text;
            user.Email       = txtEmail.Text;
            user.Address     = txtAddress.Text;
            user.Sex         = (rbtnFemale.Checked) ? false : true;
            user.UserGroupId = Convert.ToInt32(cbbUserGroup.SelectedValue);
            user.SetCreate();
            //modify date and modify by is null
            //add user
            try
            {
                using (IUnitOfWork uow = new UnitOfWork())
                {
                    uow.UsersRepository.Add(user);
                    uow.Commit();
                }
            } catch
            {
                lblNotify1.SetText(UI.createfailed, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            //Create Staff
            Staff staff = new Staff(user);

            staff.StaffCode = txtCode.Text;
            staff.StaffName = txtFullName.Text;
            //staff.Phone = txtPhone.Text;
            //staff.Email = txtEmail.Text;
            staff.Fax          = txtFax.Text;
            staff.Skype        = txtSkype.Text;
            staff.Birthday     = dtpBirthday.DateTime;
            staff.BeginDate    = dtpBeginDate.DateTime;
            staff.DepartmentId = Convert.ToInt32(cbbDepartment.SelectedValue);
            staff.PositionId   = Convert.ToInt32(cbbPosition.SelectedValue);
            staff.Avatar       = ptAvatar.Image != null?PictureUtility.getFileName(ptAvatar.Tag.ToString()) : "";

            staff.SignaturePhoto = ptSignature.Image != null?PictureUtility.getFileName(ptSignature.Tag.ToString()) : "";

            staff.SetCreate();
            //modify date and modify by is null
            try
            {
                using (IUnitOfWork uow = new UnitOfWork())
                {
                    staff.UserId = uow.UsersRepository.Find(user.UserName);
                    uow.StaffRepository.Add(staff);
                    uow.Commit();
                }
            } catch
            {
                lblNotify1.SetText(UI.createfailed, ToolBoxCS.LabelNotify.EnumStatus.Failed);
                return;
            }
            if (AddnewStaff != null)
            {
                AddnewStaff(staff);
            }
            Close();
        }