2003 BMW 325i

When my check engine light comes on the car stops shifting and hardly moves. If I clear the check engine code I can drive for 50 miles until the light comes back and I start having these problems.