Example #1
0
        public bool Delete(int Id)
        {
            var sql    = $"Delete from Major where Id = @Id;";
            var sqlCmd = new SqlCommand(sql, Connection.sqlConnection);

            sqlCmd.Parameters.AddWithValue("@Id", Id);
            try {
                int rowsAffected = sqlCmd.ExecuteNonQuery();
                switch (rowsAffected)
                {
                case 0: return(false);

                case 1: return(true);

                default: throw new Exception($"Error: deleted {rowsAffected} rows");
                }
                //if (rowsAffected == 1) return true;
                ////one row deleted
                //if (rowsAffected == 0) return false;
                ////nothing deleted
                //throw new Exception($"Error: deleted {rowsAffected} rows");
                ////any other amount of rows deleted, showing a lot more than expected was deleted- purpose of exception
            }
            catch (SqlException ex)
            {
                var refIntEx = new Exceptions.ReferentialIntegrityException("Cannot delete major used by student", ex);
                throw refIntEx;
                //trapping sql exception, adding our own exception while showing user the sql exception?
            }
        }
Example #2
0
        public bool Delete(int Id)
        {
            var sql = $"DELETE from Major Where Id = @Id;";
            var cmd = new SqlCommand(sql, Connection.sqlConnection);

            cmd.Parameters.AddWithValue("@Id", Id);

            try {
                // need to catch RefIntgrity exception
                int rowsAffected = cmd.ExecuteNonQuery();

                return(CheckRowsAffected(rowsAffected));
            } catch (SqlException ex) {
                var refIntEx
                    = new Exceptions.ReferentialIntegrityException("Cannot delete major used by student", ex);
                throw refIntEx;
            }
        }