Esempio n. 1
0
        private void btnDeleteStudent_Click(object sender, EventArgs e)
        {
            //ensure a student is selected
            if (lstStudentRoster.SelectedIndex < 0)
            {
                MessageBox.Show("Please select a student");
                return;
            }

            DialogResult res = MessageBox
                               .Show("Are you sure?",
                                     string.Empty,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Warning);

            if (res == DialogResult.Yes)
            {
                //get selected student
                Student selStu =
                    lstStudentRoster.SelectedItem as Student;

                //delete student by SID
                if (StudentDB.DeleteStudent(selStu.StudentID))
                {
                    MessageBox.Show($"{selStu.FirstName} was deleted");
                    //PopulateStudentListBox();

                    //This removes the student from the listbox
                    //and is more efficient then talking to DB again
                    lstStudentRoster.Items.Remove(selStu);
                }
            }
        }
Esempio n. 2
0
        private void PopulateStudentListBox()
        {
            List <Student> stuList =
                StudentDB.GetAllStudents();

            lstStudentRoster.Items.Clear();
            foreach (Student s in stuList)
            {
                lstStudentRoster.Items.Add(s);
            }
        }
Esempio n. 3
0
        private void btnUpdateStudent_Click(object sender, EventArgs e)
        {
            //update student with new data
            stu.FirstName   = txtName.Text;
            stu.Major       = txtMajor.Text;
            stu.DateOfBirth =
                Convert.ToDateTime(txtDateOfBirth.Text);

            if (StudentDB.UpdateStudent(stu))
            {
                MessageBox.Show("Updated!");
                Close();
            }
        }
Esempio n. 4
0
        private void btnAddStudent_Click(object sender, EventArgs e)
        {
            if (IsDataValid())
            {
                string   name  = txtName.Text;
                DateTime dob   = Convert.ToDateTime(txtDateOfBirth.Text);
                string   major = txtMajor.Text;

                //add all data to student object
                //METHOD 1: POPULATE OBJECT PROPERTY BY PROPERTY
                var addStu = new Student();
                addStu.FirstName   = name;
                addStu.DateOfBirth = dob;
                addStu.Major       = major;
                addStu.StudentID   = txtStudentID.Text;
                ////database will generate SID
                ////METHOD 2: Object Initialization Syntax
                //var addStu2 = new Student()
                //{
                //    FirstName = name,
                //    DateOfBirth = dob,
                //    Major = major
                //};

                //METHOD 3: Use the constructor!
                //var addStu3 =
                //    new Student(name, dob, major);

                //add student object to database
                if (StudentDB.AddStudent(addStu))
                {
                    MessageBox.Show("Student Added!");

                    //ignore passing the name back to the
                    //main form
                    this.Tag         = name;
                    this.StudentName = name;
                    this.stu         = name;

                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("Stu cannot be added at this time!");
                }
            }
        }