Hello Venom0706
This happens because of the difference in the electric charges between you and your device. As you said that your laptop was connected to the charger when you faced the issue, it can be due to two reasons.
1) The laptop might not be connected to the grounded outlet.
2) You might have touched the laptop after having rubbed your cloths that you may be wearing (typically your socks) against a carpeted floor.
This can be harmful for the internal (minute) components of your laptop in a long run, but once in a while, the laptops can bear such inductions.
To avoid this in future, you can follow the below approaches:
• Check if there is proper grounding done to the power outlets in your home and work place, and always connect your laptop to a grounded outlet.
• You can place an antistatic floor mat at your work place and home.
• In case of improper grounding, before touching any internal components of the laptop, touch any metal object with your hand. This will ground you, hence neutralizing any static charge that your body may have gained.
Hope this helps.