BTI bacteria works very well too.
Mosquito bits and
Dunks are two popular products that contain BTI and work great. I use Bits myself every now and then with good results. Bits are recommended for indoors. Dunks are recommended for ponds outdoors, but work just the same.
The bits are supposed to be submerged in your nutrient solution for at least an hour, before you water your plants. After an hour, you can remove the bits and your nutrient solution will be full of BTI bacteria. It is harmless for humans and plants and will only be harmful to mosquitos. The BTI will be active in your grow medium for up to 30 days and all larvae living in the medium will ingest this bacteria, which will kill them.
BTI kills the larvae, preventing new generations of gnats, and yellow sticky traps will handle the adult gnats flying around.
Good luck!