private void create_compound_btn_Click(object sender, EventArgs e) { string ionicCompound, ionicName; string errorMessage = ""; int cationNum, anionNum; // Colors Color yellow = Color.FromArgb(211, 255, 64); Color red = Color.FromArgb(255, 54, 54); /** Cation properties **/ Ion cation = new Ion(); cation.ionAbbr = cation_input.Text; convertChargeToInt(cationChg_txt.Text, out cation.ionCharge, 1, ref errorMessage); /** Anion properties **/ Ion anion = new Ion(); anion.ionAbbr = anion_input.Text; convertChargeToInt(anionChg_txt.Text, out anion.ionCharge, -1, ref errorMessage); // These connect to the database and determines whether the ion entered exists or not. // This also saves the name of the ion to the object property ionName. conn.queryIonDB(ref cation, "cation", ref errorMessage); conn.queryIonDB(ref anion, "anion", ref errorMessage); // Function determines how many anions and cations are needed to form the ionic compound. findIonNums(cation.ionCharge, anion.ionCharge, out cationNum, out anionNum); ionicCompound = formatIonAbbr(cation.ionAbbr) + displayIonNumber(cationNum) + formatIonAbbr(anion.ionAbbr) + displayIonNumber(anionNum); ionicName = cation.ionName + " " + anion.ionName; // Validation successful, and ionic compound was formed. if (errorMessage == "") { ionicCompound_label.Text = ionicCompound; ionicCompound_label.ForeColor = yellow; ionicName_label.Text = ionicName; // Validation unsuccessful, and ran into an error. } else { ionicCompound_label.Text = "Error"; ionicCompound_label.ForeColor = red; ionicName_label.Text = errorMessage; ionicName_label.Font = new Font("Bahnschrift", 14); } }
// // This submits ion data to the ion table. // public bool SubmitIonDB(Ion i) { bool result = false; FormattableString sql = $"INSERT INTO ions (`abbreviation`, `name`, `charge`, `type`) VALUES ('{i.abbr}', '{i.name}', '{i.charge}', '{i.ionType}');"; try { MySqlCommand cmd = new MySqlCommand(sql.ToString(), conn); int rowsAffected = cmd.ExecuteNonQuery(); result = (rowsAffected > 0) ? true : false; } catch (Exception ex) { // Better handle exception // Console.WriteLine("{0}", ex); } return(result); }
private void btnSubmit_Click(object sender, EventArgs e) { Ion i = new Ion(); i.ionAbbr = txtbxIonAbbr.Text; i.ionName = txtbxIonName.Text; i.ionCharge = Convert.ToInt32(txtbxIonCharge.Text); i.ionType = txtbxIonType.Text; if (conn.SubmitIonDB(i)) { lblResult.Text = "Success"; txtbxIonAbbr.Text = ""; txtbxIonCharge.Text = ""; txtbxIonName.Text = ""; txtbxIonType.Text = ""; } else { lblResult.Text = "Error"; } //sending everything to the database with the SubmitDB that jorge created }
// // This queries the database for a specific ion by abbreviation, charge, and type. It's used to validate the existence of the ion. // public void queryIonDB(ref Ion userIon, string ionType, ref string errorMessage) { bool result = false; string query = $"SELECT * FROM ions " + $"WHERE abbreviation = '{userIon.abbr}'" + $"AND charge = '{userIon.charge}'" + $"AND type = '{ionType}'"; try { MySqlCommand cmd = new MySqlCommand(query, conn); MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { userIon.name = reader["name"].ToString(); } result = true; } if (result == false) { errorMessage = "Ion not found. The " + ionType + " you entered either doesn't exist, is in the wrong category, or perhaps you misspelled it."; } reader.Close(); } catch (Exception ex) { errorMessage = "Encountered an issue with the database. Try again." + ex; } }