예제 #1
0
        //UserInfo copy to Database
        private void listenClient_OnNewUser(UserInfoModel user)
        {
            var userModel = UserInfoBll.Get(user.PIN);
            int nRtn      = 0;

            if (null == userModel)
            {
                nRtn = _userInfoBll.Add(user);
            }
            else
            {
                nRtn = _userInfoBll.Update(user);
            }
            if (nRtn > 0 && _currentPageId == (int)PageIdEnum.User && m_lastfrm != null)
            {
                ((UCUser)m_lastfrm).LoadAllUsers();
            }
        }
        /// <summary>
        /// 保存用户按钮事件
        /// </summary>

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPin.Text))
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text    = "Please input user id.";
                return;
            }
            lblMsg.Visible = false;
            UserInfoModel user = _bll.Get(txtPin.Text.Trim());  //数据库中获取对应pin的用户信息

            if (user == null)
            {
                user = new UserInfoModel();
            }
            user.PIN      = txtPin.Text;
            user.UserName = txtUserName.Text;
            user.Passwd   = txtPassword.Text;
            user.IDCard   = txtCard.Text;
            user.Pri      = cmbPrivilege.SelectedValue.ToString();

            int ret = 0;

            try
            {
                //数据库中没有对应的用户,则增加
                if (user.ID == 0)
                {
                    ret = _bll.Add(user);
                    if (ret > 0)
                    {
                        LoadAllUsers();  //更新用户列表
                        this.lblMsg.Visible = true;
                        this.lblMsg.Text    = "Add successful.";
                    }
                    else
                    {
                        this.lblMsg.Visible = true;
                        this.lblMsg.Text    = "Add fail.";
                    }
                }
                else
                {
                    //数据库找到对应的用户,则更新
                    ret = _bll.Update(user);
                    if (ret > 0)
                    {
                        LoadAllUsers();  //更新用户列表
                        this.lblMsg.Visible = true;
                        this.lblMsg.Text    = "Update successful.";
                    }
                    else
                    {
                        this.lblMsg.Visible = true;
                        this.lblMsg.Text    = "Update fail.";
                    }
                }

                if (ret > 0)
                {
                    //保存用户photoiD
                    string strDesPath = "";
                    strDesPath = System.Environment.CurrentDirectory + "\\Photo\\" + txtPin.Text + ".jpg";

                    if (picUserPhoto.ImageLocation != null)
                    {
                        File.Copy(picUserPhoto.ImageLocation, strDesPath, true); //notice,picturebox的ImageLocation使用前,需要手动给他附上值
                    }
                    txtPin.Enabled = false;                                      //保存了之后pin不可再编辑
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text    = ex.ToString();
            }
        }