System.DirectoryServices Unknown error (0x80005000) Resolution

This seems completely ridiculous.  I needed to write some queries against Active Directory so I made a quick console application to thresh things out before putting them inside SharePoint.  After getting everything working, I dropped it into a web part and deployed it out to our test server.  After setting up the LDAP connection on the web part (I made them editable properties instead of hardcoding them) I ended up getting the following obscure error:

COM Exception was unhandled
Unknown error (0x80005000)
Error Code: -2147463168
Source: System.DirectoryServices

At first I was thinking it was a SharePoint induced security problem and was expecting a long and arduous road of getting everything working.  However, after finding a random comment in a forum it turns out this is really simple issue with a terrible error message. 

Resolution:

Capitalize LDAP:// in the connection string.

It turns out I accidentally did everything right in the console app but failed to do it again when configuring my web part.  I’m glad I found this now instead of running into it while deploying it to production.  What I decided to do was just add this code to the set section of my web part property:

Regex.Replace(value, “ldap://”, “LDAP://”, RegexOptions.IgnoreCase);

It uses a regular expression to locate ldap:// in the connection string regardless of how it is spelled (Ldap, LDap, ldaP, etc) and replaces it with the fully uppercased version. Pretty easy fix.