private void bAdd_Click(object sender, EventArgs e) { Allergy allergy = new Allergy(); allergy.Name = tbName.Text; bool successfull = true; try { successfull = mDatabaseConnection.ExecuteNonReturnQuery(allergy.GetInsertQuery()); } catch (SqlException ex) { successfull = false; MessageBox.Show(ex.Message, "Error:" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Error); } if (successfull) { MessageBox.Show("Het allergy is toegevoegd.", "Toevoegen gelukt."); mForm.ReloadAllergies(); this.Close(); } }
public List<Allergy> GetAllAllergies() { string query = "SELECT * FROM allergies"; //SqlCommand mSqlCommand = new SqlCommand(query, mSqlConnection); OleDbCommand command = new OleDbCommand(query, mConnection); //SqlDataReader mSqlDataReader = null; OleDbDataReader dataReader = null; List<Allergy> allergyList = new List<Allergy>(); try { //mSqlConnection.Open(); //mSqlDataReader = mSqlCommand.ExecuteReader(); mConnection.Open(); dataReader = command.ExecuteReader(); // Check is the reader has any rows at all before starting to read. //if (mSqlDataReader.HasRows) if (dataReader.HasRows) { // Read advances to the next row. //while (mSqlDataReader.Read()) while (dataReader.Read()) { Allergy allergy = new Allergy(); int id = dataReader.GetInt32(dataReader.GetOrdinal("id")); string name = dataReader.GetString(dataReader.GetOrdinal("name")); allergy.Id = id; allergy.Name = name; allergyList.Add(allergy); } } } //catch (SqlException ex) catch (OleDbException ex) { MessageBox.Show(ex.Message, "Error:" + ex.ErrorCode, MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { //mSqlDataReader.Close(); //mSqlConnection.Close(); dataReader.Close(); mConnection.Close(); } return allergyList; }