예제 #1
0
        private bool checkFieldText()
        {
            Project_Team3.Classes.FunctionsForAllProgram funcs = new Classes.FunctionsForAllProgram();
            int idInt = -1;

            if (string.IsNullOrWhiteSpace(this.textBox1.Text))
            {
                MessageBox.Show("The ID field cannot be empty.\n");
                return(false);
            }

            try
            {
                idInt = Convert.ToInt32(this.textBox1.Text);
            }
            catch
            {
                MessageBox.Show("ID must contain numerical digits only.");
                return(false);
            }

            if (!(funcs.ifUserIDinDatabase(idInt)))
            {
                MessageBox.Show("Such user doesn't exist.\n");
                return(false);
            }

            return(true);
        }
예제 #2
0
        private void addStudentToDatabase_Click(object sender, EventArgs e)
        {
            Classes.Student newStudent;
            Project_Team3.Classes.FunctionsForAllProgram funcs = new Classes.FunctionsForAllProgram();
            String username = this.username_box.Text;
            String id       = this.id_box.Text;
            int    id_int   = -1;

            if (checkFields() == false)
            {
                return;
            }

            try
            {
                id_int = Convert.ToInt32(id);
            }
            catch
            {
                MessageBox.Show("Unsuccessfull convertion attempt string ID to Int or ID field is empty!");
                return;
            }
            bool usernameExists = funcs.ifUserInDatabase(username);

            if (usernameExists)
            {
                MessageBox.Show("User with username " + username + " already exists");
            }

            bool userIdExists = funcs.ifUserIDinDatabase(id_int);

            if (userIdExists)
            {
                MessageBox.Show("User with id " + id_int + " already exists");
            }

            bool userExists = usernameExists || userIdExists;

            if (userExists == false)
            {
                String Name = this.firstname_box.Text, surename = this.secondName_box.Text, password = this.password_box.Text;
                int    semester;
                try
                {
                    semester = Convert.ToInt32(this.semester_box.Text);
                    if (semester < 0 || semester > 8)
                    {
                        MessageBox.Show("Semester value must been 1-8");
                        return;
                    }
                }
                catch
                {
                    semester = 1;
                }
                string email       = this.email_Box.Text;
                bool   emailExists = funcs.ifEmailInDatabase(email);
                if (emailExists)
                {
                    MessageBox.Show("User with email " + email + " already exists");
                    return;
                }
                newStudent = new Classes.Student(id, username, Name, surename, password, semester, email);
                DBconnect db = new DBconnect();
                try
                {
                    db.addStudentToDB(newStudent);
                    MessageBox.Show("New Student been added to database!");
                }
                catch
                {
                    MessageBox.Show("Something went wrong, user not been added! \ncheck your database connection and fields correctness and try again!");
                }
            }
        }