Esempio n. 1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtWorkNumber.Text.Trim() == "")
                {
                    MessageBox.Show("工号不能为空,请输入。");
                    this.txtWorkNumber.Focus();
                    return;
                }

                if (this.txtWorkNumber.Text.ToLower() == "admin")
                {
                    MessageBox.Show("工号不能为[admin],请重新输入。");
                    this.txtWorkNumber.Text = "";
                    this.txtWorkNumber.Focus();
                    return;
                }

                if (this.txtName.Text.Trim() == "")
                {
                    MessageBox.Show("姓名不能为空,请输入。");
                    this.txtName.Focus();
                    return;
                }

                if (this.txtName.Text.ToLower() == "admin")
                {
                    MessageBox.Show("姓名不能为[admin],请重新输入。");
                    this.txtName.Text = "";
                    this.txtName.Focus();
                    return;
                }

                var userName   = this.txtName.Text;
                var workNumber = this.txtWorkNumber.Text;
                var dept       = this.cbbDept.SelectedIndex != -1 ? this.cbbDept.SelectedItem.ToString() : "";

                if (this.isAdd)
                {
                    DataRow[] row = Global.DtUser.Select(string.Format("workNumber = '{0}'", this.txtWorkNumber.Text));

                    if (row.Length > 0)
                    {
                        MessageBox.Show("此工号已经存在,请重新输入。");
                        this.txtWorkNumber.Text = "";
                        this.txtWorkNumber.Focus();
                        return;
                    }

                    var id = Guid.NewGuid().ToString();

                    DataRow dr = Global.DtUser.NewRow();
                    dr["id"]          = id;
                    dr["userName"]    = this.txtName.Text;
                    dr["workNumber"]  = this.txtWorkNumber.Text;
                    dr["dept"]        = this.cbbDept.SelectedItem.ToString();
                    dr["password"]    = "******";
                    dr["permissions"] = "";

                    Global.DtUser.Rows.Add(dr);

                    if (XmlFile.AddXmlNode(Global.UsersConfigPath, dr))
                    {
                        MessageBox.Show("添加成功。");
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("添加失败。");
                    }
                }
                else
                {
                    var id = this.dgvRow.Cells["id"].Value.ToString();

                    DataRow[] dr = Global.DtUser.Select(string.Format("id = '{0}'", id));

                    if (dr.Length > 0)
                    {
                        dr[0]["userName"]   = this.txtName.Text;
                        dr[0]["workNumber"] = this.txtWorkNumber.Text;
                        dr[0]["dept"]       = this.cbbDept.SelectedItem.ToString();
                    }

                    Dictionary <string, string> dictAttribute = new Dictionary <string, string>();

                    dictAttribute.Add("dept", dr[0]["dept"].ToString());
                    dictAttribute.Add("userName", dr[0]["userName"].ToString());

                    var userID = dr[0]["id"].ToString();

                    if (XmlFile.UpdateXmlNode(Global.UsersConfigPath, userID, dictAttribute))
                    {
                        MessageBox.Show("修改成功。");
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败。");
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriterExceptionLog(ex.ToString());
            }
        }