// It registers the new user as long as all controls hold text private void buttonRegister_Click(object sender, EventArgs e) { //variables to be used string username = "", password = "", password2 = "", firstname = "", lastname = "", email = "", clientType = "", userType = ""; Int32 phone = 0; //It checks that the text and combo boxes have something typed in them bool hasText = checkTextControls(); if (!hasText) { MessageBox.Show("Please make sure all fields have data."); comboBoxUserType.Focus(); return; } //It checks that the username doesn't exist yet in the DB bool userOK = checkUsername(textBoxUserName.Text.Trim(), comboBoxUserType.Text); if (!userOK) { MessageBox.Show("This user already exists in the Data Base, please try another one."); textBoxUserName.BackColor = Color.LightCoral; textBoxUserName.Focus(); return; } //It checks that the password==password2 bool passOK = checkPassword(textBoxPassword.Text.Trim(), textBoxPassword2.Text.Trim()); if (!passOK) { MessageBox.Show("Please make sure the password in both password fields is the same."); textBoxPassword.BackColor = Color.LightCoral; textBoxPassword2.BackColor = Color.LightCoral; textBoxPassword.Focus(); return; } //It checks that the email is a proper one bool emailOK = checkEmail(textBoxEmail.Text.Trim()); if (!emailOK) { MessageBox.Show("Please insert a proper email in the field."); textBoxEmail.BackColor = Color.LightCoral; textBoxEmail.Focus(); return; } //It checks that the phone number introduced is numbers if the control is enabled if (textBoxPhone.Enabled == true) { bool phoneOK = int.TryParse(textBoxPhone.Text.Trim(), out phone); if (!phoneOK) { MessageBox.Show("You can only write numbers in the phone field."); textBoxPhone.BackColor = Color.LightCoral; textBoxPhone.Focus(); return; } } //(1) GET the data from the textboxes and store into variables created above, good to put in a try catch with error message try { userType = comboBoxUserType.Text; username = textBoxUserName.Text.Trim(); password = textBoxPassword.Text.Trim(); password2 = textBoxPassword2.Text.Trim(); firstname = textBoxFirst.Text.Trim(); lastname = textBoxLast.Text.Trim(); email = textBoxEmail.Text.Trim(); switch (userType) { case "Admins": break; case "Clients": phone = int.Parse(textBoxPhone.Text.Trim()); clientType = comboBoxClientType.Text.Trim(); break; case "Instructors": phone = int.Parse(textBoxPhone.Text.Trim()); break; default: break; } } catch { //Error message, more useful when you are storing numbers etc. into the database. MessageBox.Show("Please make sure your data is in correct format."); return; } //(2) Execute the INSERT statement, making sure all quotes and commas are in the correct places. try { var insertClientQuery = ""; //It sends a different insert statement depending on the User Type Selection switch (userType) { case "Admins": insertClientQuery = $"INSERT INTO {userType} VALUES('{username}', '{password}', '{firstname}', '{lastname}', '{email}')"; break; case "Clients": insertClientQuery = $"INSERT INTO {userType} VALUES('{username}', '{password}', '{firstname}', '{lastname}', '{email}', {phone}, '{clientType}')"; break; case "Instructors": insertClientQuery = $"INSERT INTO {userType} VALUES('{username}', '{password}', '{firstname}', '{lastname}', '{email}', {phone})"; break; default: break; } SQL.executeQuery(insertClientQuery); } catch (Exception) { MessageBox.Show("Register attempt unsuccessful. Check insert statement. Could be a Username conflict too."); return; } //success message for the user to know it worked MessageBox.Show("Successfully Registered as " + userType + ": " + firstname + " " + lastname + ". Your username is: " + username); //Go back to the login page since we registered successfully to let the user log in Hide(); //hides the register form LoginPage login = new LoginPage(); //creates the login page as an object login.ShowDialog(); //shows the new login page form this.Close(); //closes the register form that was hidden }