Beispiel #1
0
        /// <summary>
        /// Button to submit information about a new student, or an existing student. Checks for input validity, and checks whether the user is editing or not
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //Create variables and initialize them
            int    id         = 0;
            String name       = "";
            String gender     = "";
            String dob        = "";
            String dept       = "";
            string contactNum = "";

            //If the user is not editing (adding a new student, then collect their information)
            if (editOrAdd != "Edit")
            {
                //If input is correct, add the user
                if (checkInput())
                {
                    //Collect all information from form
                    id         = Int32.Parse(txtID.Text);
                    name       = txtName.Text;
                    gender     = cmbGender.Text;
                    dob        = dtpDOB.Text;
                    dept       = cmbDepartments.Text;
                    contactNum = txtContactNum.Text;

                    //Insert all that information into a student object
                    Student newStudent = new Student(id, name, gender, dob, dept, contactNum);

                    //Insert the student into the databse
                    LibraryDB enterNewStudent = new LibraryDB();
                    enterNewStudent.insertEditStudent(newStudent, "Add");

                    DialogResult dialogResult = MessageBox.Show("Student has been successfully added!\nWould you like to add another?", "Student Added", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                    //if they choose yes, refresh the form
                    if (dialogResult == DialogResult.Yes)
                    {
                        RefreshForm();
                    }
                    else if (dialogResult == DialogResult.No) //If they choose no, exit the form
                    {
                        OpenDash();
                        this.Close();
                    }
                }
            }
            else //If user is editing information, use an UPDATE statement, not an INSERT statement
            {
                //Collect all information from form
                id         = Int32.Parse(txtID.Text);
                name       = txtName.Text;
                gender     = cmbGender.Text;
                dob        = dtpDOB.Text;
                dept       = cmbDepartments.Text;
                contactNum = txtContactNum.Text;

                //Insert all that information into a student object
                Student newInfo = new Student(id, name, gender, dob, dept, contactNum);

                //Insert the student into the databse
                LibraryDB editStudent = new LibraryDB();
                editStudent.insertEditStudent(newInfo, "Edit");

                DialogResult dialogResult = MessageBox.Show("Students information has successfully been updated!", "Student Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);

                OpenDash();
                this.Close();
            }
        }