Example #1
0
        /// <summary>
        /// 修改学生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            #region 判断信息是否为空
            //判断信息是否为空
            if (txtStudentNumber.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生姓名!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtStudentNumber.Focus();
                return;
            }
            if (txtStudentName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生姓名!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtStudentName.Focus();
                return;
            }
            if (txtIDnumber.Text.Length == 0)
            {
                MessageBox.Show("请输入身份证号!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtIDnumber.Focus();
                return;
            }
            if (this.combCollageName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择学院!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.combCollageName.Focus();
                return;
            }
            if (this.combSpecialityName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择专业!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.combSpecialityName.Focus();
                return;
            }
            if (this.combClassName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择班级!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.combClassName.Focus();
                return;
            }
            if (txtStudentNation.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生民族!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtStudentNation.Focus();
                return;
            }
            if (pictureBoxStudentPhoto.Image == null)
            {
                MessageBox.Show("请添加学生照片!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.linkLabelPicture.Focus();
                return;
            }
            //验证身份证号是否符合要求
            if (!DateValidate.IsIDCard(this.txtIDnumber.Text.Trim()))
            {
                MessageBox.Show("身份证号不符合格式要求!", "验证提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            //验证身份证号是否和出生日期吻合
            string month = string.Empty;
            string day   = string.Empty;
            if (Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month < 10)
            {
                month = "0" + Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month;
            }
            else
            {
                month = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month.ToString();
            }
            if (Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day < 10)
            {
                day = "0" + Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day;
            }
            else
            {
                day = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day.ToString();
            }
            string birthday = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Year.ToString() + month + day;

            if (!this.txtIDnumber.Text.Trim().Contains(birthday))
            {
                MessageBox.Show("身份证号和出生日期不匹配!", "修改提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            ////判断身份证号是否重复
            if (this.objStudentService.IsIDNumberNoExisted(this.txtIDnumber.Text.Trim(), this.txtStudentNumber.Text.Trim()))
            {
                MessageBox.Show("身份证号不能和现有学生身份证号重复!", "修改提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            #endregion
            //封装学生对象
            Student objStudent = new Student()
            {
                StudentNumber      = txtStudentNumber.Text.Trim(),
                StudentName        = txtStudentName.Text.Trim(),
                StudentSex         = combStudentSex.Text.Trim(),
                ClassName          = combClassName.Text.Trim(),
                ClassID            = Convert.ToInt32(combClassName.SelectedValue),
                StudentBrithday    = dateTimeStudentBrithday.Value,
                StudentNativeplace = txtStudentNativeplace.Text.Trim(),
                StudentPhoneNumber = txtStudentPhoneNumber.Text.Trim(),
                StudentNation      = txtStudentNation.Text.Trim(),
                StudentPolitical   = combStudentPolitical.Text.Trim(),
                SpecialityName     = combSpecialityName.Text.Trim(),
                IDnumber           = txtIDnumber.Text.Trim(),
                //StudentPwd = Commons.EncodeHelper.AES_Encrypt(objStudent.IDnumber.Substring(12, 6)),
                StudentCollage = combCollageName.Text.Trim(),
                StudentAddress = txtStudentAddress.Text.Trim(),
                StudentJob     = combStudentJob.Text.Trim(),
                StudentPhoto   = this.pictureBoxStudentPhoto.Image == null ? "" : new SerializeObjectToString().SerializeObject(this.pictureBoxStudentPhoto.Image)
            };
            //提交对象
            //判断是否保存成功

            try
            {
                int result = objStudentService.UpdateStudent(objStudent);
                if (result == 1)
                {
                    MessageBox.Show("学生信息修改成功!", "修改提示");
                    this.DialogResult = DialogResult.OK;
                    this.txtStudentNumber.Focus();
                }
                else
                {
                    MessageBox.Show("学生信息修改失败,不可以更改学生学号!", "添加提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #2
0
        //添加学生
        private void btnadd_Click(object sender, EventArgs e)
        {
            #region 判断信息是否为空
            //判断信息是否为空
            if (txtStudentNumber.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生姓名!", "信息提示");
                this.txtStudentNumber.Focus();
                return;
            }
            if (txtStudentName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生姓名!", "信息提示");
                this.txtStudentName.Focus();
                return;
            }
            if (this.combStudentSex.SelectedIndex == -1)
            {
                MessageBox.Show("请选择性别!", "信息提示");
                this.combStudentSex.Focus();
                return;
            }
            if (txtIDnumber.Text.Length == 0)
            {
                MessageBox.Show("请输入身份证号!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtIDnumber.Focus();
                return;
            }
            if (this.combCollageName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择学院!", "信息提示");
                this.combCollageName.Focus();
                return;
            }
            if (this.combSpecialityName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择专业!", "信息提示");
                this.combSpecialityName.Focus();
                return;
            }
            if (this.combClassName.SelectedIndex == -1)
            {
                MessageBox.Show("请选择班级!", "信息提示");
                this.combClassName.Focus();
                return;
            }
            if (this.combStudentJob.SelectedIndex == -1)
            {
                MessageBox.Show("请选择学生职务!", "信息提示");
                this.combStudentJob.Focus();
                return;
            }
            if (this.combStudentPolitical.SelectedIndex == -1)
            {
                MessageBox.Show("请选择政治面貌!", "信息提示");
                this.combStudentPolitical.Focus();
                return;
            }
            if (txtStudentNation.Text.Trim().Length == 0)
            {
                MessageBox.Show("请填写学生民族!", "信息提示");
                this.txtStudentNation.Focus();
                return;
            }
            if (pictureBoxStudentPhoto.Image == null)
            {
                MessageBox.Show("请添加学生照片!", "信息提示");
                this.linkLabelPicture.Focus();
                return;
            }
            //验证身份证号是否符合要求
            if (!DateValidate.IsIDCard(this.txtIDnumber.Text.Trim()))
            {
                MessageBox.Show("身份证号不符合格式要求!", "验证提示");
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            //验证身份证号是否和出生日期吻合
            string month = string.Empty;
            string day   = string.Empty;
            if (Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month < 10)
            {
                month = "0" + Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month;
            }
            else
            {
                month = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Month.ToString();
            }
            if (Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day < 10)
            {
                day = "0" + Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day;
            }
            else
            {
                day = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Day.ToString();
            }
            string birthday = Convert.ToDateTime(this.dateTimeStudentBrithday.Text).Year.ToString() + month + day;

            if (!this.txtIDnumber.Text.Trim().Contains(birthday))
            {
                MessageBox.Show("身份证号和出生日期不匹配!", "验证提示");
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            //判断身份证号是否重复
            if (this.objStudentService.IsIDNumberExisted(this.txtIDnumber.Text.Trim()))
            {
                MessageBox.Show("身份证号已经存在!", "验证提示");
                this.txtIDnumber.Focus();
                this.txtIDnumber.SelectAll();
                return;
            }
            //判断学号是否重复
            if (this.objStudentService.IsStudentNumberExisted(this.txtStudentNumber.Text.Trim()))
            {
                MessageBox.Show("学号已经存在!", "验证提示");
                this.txtStudentNumber.Focus();
                this.txtStudentNumber.SelectAll();
                return;
            }
            #endregion

            //封装学生对象
            Student objStudent = new Student()
            {
                StudentNumber      = txtStudentNumber.Text.Trim(),
                StudentName        = txtStudentName.Text.Trim(),
                StudentSex         = combStudentSex.Text.Trim(),
                ClassName          = combClassName.Text.Trim(),
                ClassID            = Convert.ToInt32(combClassName.SelectedValue),
                StudentBrithday    = dateTimeStudentBrithday.Value,
                StudentNativeplace = txtStudentNativeplace.Text.Trim(),
                StudentPhoneNumber = txtStudentPhoneNumber.Text.Trim(),
                StudentNation      = txtStudentNation.Text.Trim(),
                StudentPolitical   = combStudentPolitical.Text.Trim(),
                SpecialityName     = combSpecialityName.Text.Trim(),
                IDnumber           = txtIDnumber.Text.Trim(),
                //StudentPwd = Commons.EncodeHelper.AES_Encrypt(objStudent.IDnumber.Substring(12, 6)),
                StudentCollage = combCollageName.Text.Trim(),
                StudentAddress = txtStudentAddress.Text.Trim(),
                StudentJob     = combStudentJob.Text.Trim(),
                StudentPhoto   = this.pictureBoxStudentPhoto.Image == null ? "" : new SerializeObjectToString().SerializeObject(this.pictureBoxStudentPhoto.Image)
            };

            //提交对象
            //判断是否保存成功

            try
            {
                int result = objStudentService.AddStudent(objStudent);
                if (result == 1)
                {
                    DialogResult dresult = MessageBox.Show("添加成功!是否继续添加", "添加询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dresult == DialogResult.OK)
                    {
                        //清空当前的文本框
                        foreach (Control item in this.tlpStudentAdd.Controls)
                        {
                            if (item is TextBox)
                            {
                                item.Text = "";
                            }
                            else if (item is ComboBox)
                            {
                                ((ComboBox)item).SelectedIndex = -1;
                            }
                            else if (item is PictureBox)
                            {
                                ((PictureBox)item).Image = null;
                            }
                            else if (item is DateTimePicker)
                            {
                                ((DateTimePicker)item).Value = DateTime.Now;
                            }
                        }
                        this.txtStudentNumber.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("添加失败!", "添加提示");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }