private void buttonAddAllergy_Click(object sender, EventArgs e) { Allergy allergy = new Allergy(); textBoxAllergyName.Text = textBoxAllergyName.Text.Trim(); if ((textBoxAllergyName.Text.Length > 1)) { allergy.AllergyName = textBoxAllergyName.Text; try { _controller.AddAllergy(allergy); MessageBox.Show(@"Allergy Added."); Close(); } catch (Exception exception) { MessageBox.Show(@"There was a problem adding the allergy." + exception); } } else { MessageBox.Show(@"Allergy Name is required field!. Please provide the information and try again!"); } }
/// <summary> /// Add's an allergy to the db /// </summary> /// <param name="allergy">The allergy to be added</param> public static void AddAllergy(Allergy allergy) { const string insertStatement = "INSERT into allergies " + " (allergy_name, enabled) " + " values(@allergy_name, @enabled)"; try { using (SqlConnection connection = NorthwindDbConnection.GetConnection()) { connection.Open(); using (SqlCommand insertCommand = new SqlCommand(insertStatement, connection)) { insertCommand.Parameters.AddWithValue("@allergy_name", allergy.AllergyName); insertCommand.Parameters.AddWithValue("@enabled", 1); insertCommand.ExecuteNonQuery(); } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } }
/// <summary> /// Returns all allergies from the database /// </summary> /// <returns></returns> public static List<Allergy> GetAllAllergies() { List<Allergy> allergyList = new List<Allergy>(); const string selectStatement = "Select * from allergies"; try { using (SqlConnection connection = NorthwindDbConnection.GetConnection()) { connection.Open(); using (SqlCommand selectCommand = new SqlCommand(selectStatement, connection)) { using (SqlDataReader reader = selectCommand.ExecuteReader()) { while (reader.Read()) { Allergy allergy = new Allergy(); allergy.AllergyID = (Int32)reader["allergyID"]; allergy.AllergyName = reader["allergy_name"].ToString().Trim(); allergy.Enabled = (Byte)reader["enabled"]; allergyList.Add(allergy); } } } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } return allergyList; }
/// <summary> /// Checks the DB to see if an allergy has been used /// </summary> /// <param name="allergyId"></param> /// <returns>true or false</returns> private static Boolean checkAllergyPresence(int allergyId) { Boolean presence = false; Allergy check = new Allergy(); String checkStatement = "Select a.allergyID from allergies a join patient_allergies p on a.allergyID = p.allergyID where p.allergyID = @allergyID"; try { using (SqlConnection connection = NorthwindDbConnection.GetConnection()) { connection.Open(); using (SqlCommand selectCommand = new SqlCommand(checkStatement, connection)) { selectCommand.Parameters.AddWithValue("@allergyID", allergyId); using (SqlDataReader reader = selectCommand.ExecuteReader()) { while (reader.Read()) { check.AllergyID = (Int32)reader["allergyID"]; } } if (check.AllergyID != 0) { presence = true; } } } } catch (SqlException ex) { throw ex; } catch (Exception ex) { throw ex; } return presence; }
internal void AddAllergy(Allergy allergy) { AllergyDAL.AddAllergy(allergy); }