Ashin
Ashin

Reputation: 139

Handling LDAP server is unavailable

private void BindSearchedUser(string Domain, string UserName)
    {
    PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
    .
    .
    .

If I provide some invalid Domain name, after executing the first line it throws an exception PrincipalServerDownException was unhandled by usercode.

Error Details: The LDAP server is unavailable.

How can I check the Domain is valid or not before executing that line So that I can Show a error msg instead of throwing the exception.

Upvotes: 0

Views: 1969

Answers (1)

Sam
Sam

Reputation: 304

You can wrap your line in a try block to catch PrincipalServerDownException, which is the exception thrown when the server cannot be reached:

private void BindSearchedUser(string Domain, string UserName)
{
    try
    {
        PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, Domain);
    } 
    catch (PrincipalServerDownException ex)
    {
        // show your error message
        return;
    }
    ...
}

Upvotes: 2

Related Questions