I never heard of a TRX400ER so I will presume it's an EX. Second gear is always the problem. Most generally, the engagement dogs are damaged from improper shifting. First, make sure you are looking at the correct gears. Second gear is the gear nearest to the sprocket end of the output shaft. The transmission is "in" second when the fourth gear on the shaft (which is fifth gear) is moved into engagement with the last gear on the shaft. The shift fork will be marked with an "L".
Second, inspect the engagement dogs on the fifth gear with and the slots on the second gear. The corners of the dogs should be crisp (not rounded and polished looking) while the slots on the second gear should have sharp, machined edges.
Third, inspect the shift fork. The fingers should be flat and have a machined appearence. Whether the fork is straight or not is less a concern than the condition of the gear dogs and the fingers where they ride in the groove of the gear.
Fourth, inspect the pin on the shift fork that rides in the groove of the shift drum. It should be round. No flat spot where it contacts the groove of the shift drum. Also inspect the nub on the shift drum where it contacts the pin on the shift fork. It should have the same machined appearence as the rest of the groove. A divot will cause the fork to not hold the gear all the way in. And don't forget to inspect the shift fork shaft. Make sure it is not bent by rolling it on a flat surface. You might also want to check the straightness of the output shaft by spinning it in a lathe or at least between two dead centers.
If there is no missing shims or snap rings; if there is no damage to the shift drum; replace both the fifth gear and the second gear on the output shaft and shift fork "L". Your problem will most likely be solved.
To prevent the problem from happening on the EX or any other machine with a constant mesh transmission, LEARN TO SHIFT PROPERLY! Either pull the clutch lever in, shift (either up or down) and hold the shift lever, then release the clutch lever. Or, get off the gas, shift and hold (without using the clutch), then get on the gas and then release the shift lever. Your transmission will last much longer.