My code will not work without connecting to the wireless AP first. Mine is designed for the ethernet shield, which has no security settings.
You obviously have a wireless setting incorrect. Check the SSID, the security type, and passphrase if needed.