/// <summary>
        /// 获取学生用户类
        /// </summary>
        /// <param name="stuno"></param>
        /// <returns></returns>
        public StudentUserModel GetStudentUser(string stuno)
        {
            var su  = PRDB.StudentUsers.SingleOrDefault(stuuser => stuuser.StuNoUserName == stuno);
            var SUM = new StudentUserModel();

            SUM.UserName = su.StuNoUserName;
            SUM.Password = su.Password;
            return(SUM);
        }
        public ActionResult ChangPassword(StudentUserModel sum, string stuno)
        {
            sum.UserName = stuno;
            string message;

            if (!sb.ModeifyPassword(sum, out message))
            {
                ModelState.AddModelError("error", message);
                return(View(sum));
            }
            else
            {
                return(RedirectToActionPermanent("MainPage", "Student", new { stuno = stuno }));
            }
        }
 /// <summary>
 /// 修改密码
 /// </summary>
 /// <param name="sum">学生用户</param>
 /// <param name="Message">修改结果消息</param>
 /// <returns></returns>
 public bool ModeifyPassword(StudentUserModel sum, out string Message)
 {
     try
     {
         var studentuser = PRDB.StudentUsers.SingleOrDefault(su => su.StuNoUserName == sum.UserName);
         if (studentuser.Password == sum.OldPassword)
         {
             studentuser.Password = sum.NewPassword;
             PRDB.SaveChanges();
             Message = "修改成功!";
             return(true);
         }
         else
         {
             Message = "旧密码不正确!";
             return(false);
         }
     }
     catch (Exception exc)
     {
         Message = exc.Message;
         return(false);
     }
 }
        private void ButtonAdd_Click(object sender, EventArgs e)
        {
            StudentUserModel student = new StudentUserModel();

            student.FirstName  = textBoxStudentFirstName.Text.Trim();
            student.LastName   = textBoxStudentLastName.Text.Trim();
            student.AcademicId = textBoxStudentId.Text;

            try
            {
                student.IsValid();
                UserController controller = new UserController();
                try
                {
                    BaseUserModel createdStudent = controller.Create(student);

                    SectionStudentController controller1 = new SectionStudentController();
                    controller1.Create(section.Id, createdStudent.Id);

                    ClassController      ccontroller = new ClassController();
                    List <ClassModel>    classList   = ccontroller.GetBySectionId(section.Id);
                    AttendanceController acontroller = new AttendanceController();

                    foreach (ClassModel Class in classList)
                    {
                        AttendanceModel attendance = new AttendanceModel();
                        attendance.StudentId = createdStudent.Id;
                        attendance.ClassId   = Class.Id;
                        try
                        {
                            acontroller.Create(attendance);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    MessageBox.Show(student.FullName + " added in " + section.SectionName);
                    var students = new FormStudentList(faculty, section);
                    students.FormClosed += new FormClosedEventHandler(dash_FormClosed);
                    students.Show();
                    this.Hide();
                }
                catch (NullReferenceException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (SQLiteException ex)
            {
                try
                {
                    UserController controller = new UserController();
                    var            gotStudent = controller.GetByAcademicId(student.AcademicId);
                    string         message    = "Student already exists in our database. Are you sure you want to add " + gotStudent.FullName + "(" + gotStudent.AcademicId + ")?";

                    DialogResult dialogResult = MessageBox.Show(message, "Confirm", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        try
                        {
                            SectionStudentController controller1 = new SectionStudentController();
                            controller1.Create(section.Id, gotStudent.Id);
                            MessageBox.Show(gotStudent.FullName + " added in " + section.SectionName);

                            var students = new FormStudentList(faculty, section);
                            students.FormClosed += new FormClosedEventHandler(dash_FormClosed);
                            students.Show();
                            this.Hide();
                        }
                        catch (SQLiteException exc)
                        {
                            if (exc.ErrorCode == 19)
                            {
                                MessageBox.Show("Error: Student already exists in this section");
                            }
                        }
                        catch (Exception exv)
                        {
                            MessageBox.Show(exv.Message);
                        }
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.Hide();
                    }
                }
                catch (Exception exc)
                {
                    MessageBox.Show(exc.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private List <StudentUserModel> GenerateStudents(DataTable dt)
        {
            List <StudentUserModel> studentList     = new List <StudentUserModel>();
            StudentUserModel        studentAtHeader = new StudentUserModel();

            try
            {
                studentAtHeader.AcademicId = dt.Columns[0].ColumnName;
                studentAtHeader.LastName   = dt.Columns[1].ColumnName.Split(' ').Last();
                int lastL = studentAtHeader.LastName.Length;
                if (studentAtHeader.LastName == dt.Columns[1].ColumnName)
                {
                    studentAtHeader.FirstName = studentAtHeader.LastName;
                }
                else
                {
                    studentAtHeader.FirstName = dt.Columns[1].ColumnName.Substring(0, dt.Columns[1].ColumnName.Length - lastL);
                }
                //Console.WriteLine("Doing header " + studentAtHeader.AcademicId + " " + studentAtHeader.LastName + ", " + studentAtHeader.FirstName);
                try
                {
                    studentAtHeader.IsValid();
                    studentList.Add(studentAtHeader);
                    //Console.WriteLine("Added header");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Invalid entry at header: " + dt.Columns[0].ColumnName + ", " + dt.Columns[1].ColumnName + ".\nError: " + ex.Message + "\nThis header will be skipped");
                }
            }
            catch
            {
                MessageBox.Show("Invalid name: " + dt.Columns[1].ColumnName);
            }


            foreach (DataRow row in dt.Rows)
            {
                StudentUserModel student = new StudentUserModel();
                try
                {
                    student.AcademicId = row[0].ToString();
                    student.LastName   = row[1].ToString().Split(' ').Last();
                    student.LastName   = student.LastName.Trim();
                    int lastLength = student.LastName.Length;
                    if (student.LastName == row[0].ToString())
                    {
                        student.FirstName = student.LastName;
                    }
                    else
                    {
                        student.FirstName = row[1].ToString().Substring(0, row[1].ToString().Length - (lastLength + 1));
                    }
                    //Console.WriteLine("Doing " + student.AcademicId + " " + student.LastName + ", " + student.FirstName);
                    try
                    {
                        student.IsValid();
                        studentList.Add(student);
                        //Console.WriteLine("Added");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Invalid entry at row: " + row[0].ToString() + ", " + row[1].ToString() + ".\nError: " + ex.Message + "\nThis row will be skipped");
                    }
                }
                catch
                {
                    MessageBox.Show("Invalid name: " + row[1].ToString() + "\n this entry will be skipped");
                }
            }

            return(studentList);
        }