private void SubmitForm() { // check that textboxes aren't empty if (userNameTextBox.Text.Length == 0 || passwordTextBox.Text.Length == 0) { MessageBox.Show("Username and password cannot have length 0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // check that username doesn't already exist using (OdbcConnection conn = new OdbcConnection(connectionString)) { conn.Open(); string query = "SELECT *\n" + "FROM ATIDelivery.dbo.CertUserLogIns\n" + "WHERE userID = '" + userNameTextBox.Text.Trim().ToLower() + "';"; OdbcCommand com = new OdbcCommand(query, conn); OdbcDataReader reader = com.ExecuteReader(); // if a row exists then username is already taken if (reader.Read()) { MessageBox.Show("Username already exists. Please select a different username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // convert passwords PasswordHash hash = new PasswordHash(passwordTextBox.Text); byte[] password = hash.ToArray(); // else submit userdata query = "INSERT INTO ATIDelivery.dbo.CertUserLogIns\n" + "VALUES (\n" + "'" + userNameTextBox.Text + "',\n" + "'" + System.Text.Encoding.Default.GetString(password).ToString() + "',\n" + "'Active',\n" + "'operator',\n" + "'" + Convert.ToByte(dataGridView1.Rows[0].Cells[0].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[1].Cells[0].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[2].Cells[0].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[3].Cells[0].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[0].Cells[1].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[1].Cells[1].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[2].Cells[1].Value) + "',\n" + "'" + Convert.ToByte(dataGridView1.Rows[3].Cells[1].Value) + "'\n" + ");"; com = new OdbcCommand(query, conn); if (com.ExecuteNonQuery() == 1) MessageBox.Show("User has been succesfully registered!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); else MessageBox.Show("Cannot commit user to database. Please contact IT support for help.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.Close(); }