The episode you are referring to is "Merry Little Christmas" in season 3. Wilson has made a deal with detective Tritter so House has a choice: jail or rehab. Dr. Cuddy and Wilson take away House's pain meds. The pain or the detox gets to be too much so House cuts himself to release endorphines. Endorphines relieve pain.