Remove the iPad from Magic Keyboard for iPad - and carefully/gently clean the smart connector contacts on both the iPad and keyboard with a soft pencil eraser. Ensure that the contacts are dirt and dust free before reconnecting the two.
If you can't connect your iPad to your Magic Keyboard, Smart Keyboard, or Smart Keyboard Folio - Apple Support
Should the problem with charging the iPad from the USB-C spine-connector of the keyboard persist, you would be best advised to visit the visit the Genius Bar at your local Apple Store or Apple Authorised Service Provider (AASP) - where the technicians will be able to test and assess your iPad and Magic Keyboard:
Genius Bar Reservation and Apple Support Options - Apple