It happens. If you're going to keep snakes, you should probably get used to being bitten. Even our most beloved family pet will taste us if we smell like food, make them feel threatened, back them into a corner, or hurt them. Snakes cannot be truly domesticated, and they will always respond to stimulation instinctively.
There can be any number of reasons why a generally docile pet might strike one day. The best I can tell you is to make sure you don't smell like food or startle your snake.
Cornsnake bites don't hurt badly. Stubbing your toe hurts WAY more than a full grown cornsnake bite...