views:

652

answers:

3

Hi,

I'm trying to find an employee in Active Directory using the following c# code:

"Select userPrincipalName, ADsPath, Department, Mail,
 HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "'
WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName.Replace("'", "''") + "'";

When I run this for an employee with a single quote in the last name (such as "O'Connor") I get the following error:

AdsDsoObject' failed with no error message available, result code: DB_E_NOTABLE(0x80040E37).

I also tried Replace("'", "\''"), nothing is working.

What am I doing wrong? need help.

Thank You!

A: 

You tried:

Replace("'", "\''")

And not:

Replace("'", "\'")

(There's an extra single quote in there).

Adam Bellaire
A: 

If i use that, single quotes are not ended properly for the select statement.

thanks!

+1  A: 

Do the replace on it's own line.

Username.Replace("'", "\'");

"Select userPrincipalName, ADsPath, Department, Mail, HomeMDB, cn, ssn FROM 
'LDAP://" + DomainName + "' WHERE objectCategory = 'person' and 
sAMAccountName = '" + UserName + "'";
jim