I was trying to execute my code but it throws exception like (Incorrect syntax near 'LDAP:'.)
This is the command text am passing
string searchRoot = "";
.
When I pass like this it throws Incorrect syntax near '<;'.
If I change my string to
string searchRoot = "LDAP://DC=yourdomain,DC=com";
then it throws "Incorrect syntax near 'LDAP:'."
What am I missing here.
Here is the command text I got it when I debug
"<LDAP://DC=yourdomain,DC=com>;
(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)));
"LDAP://DC=yourdomain,DC=com;
(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)));
sAMAccountName,
telephoneNumber,
mail, otherPager;s
ubtree"sAMAccountName, telephoneNumber, mail, otherPager;subtree"
[edit]OP Responded with code fragment - OriginalGriff[/edit]
"here is the code"
using System;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
class Program
{
static void Main(string[] args)
{
DataTable users = GetEnabledUsers();
using (SqlConnection cnx = new SqlConnection(@"Data Source=SRV03;Initial Catalog=DEV;Integrated Security=SSPI"))
{
cnx.Open();
using (SqlCommand cmd = cnx.CreateCommand())
{
foreach (DataRow user in users.Rows)
{
if (user["otherPager"] != DBNull.Value && !String.IsNullOrEmpty((string)user["otherPager"]))
{
if (user["mail"] != DBNull.Value && !String.IsNullOrEmpty((string)user["mail"]))
{
cmd.CommandText = "UPDATE Email SET EmailAddress=@mail WHERE EmployeeNumber=@empid";
cmd.Parameters.AddWithValue("@mail", user["mail"]).DbType = DbType.String;
cmd.Parameters.AddWithValue("@empid", user["otherPager"]).DbType = DbType.String;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
if (user["telephoneNumber"] != DBNull.Value && !String.IsNullOrEmpty((string)user["telephoneNumber"]))
{
cmd.CommandText = "UPDATE Phone SET PhoneNumber=@phone WHERE EmployeeNumber=@empid";
cmd.Parameters.AddWithValue("@phone", user["telephoneNumber"]).DbType = DbType.String;
cmd.Parameters.AddWithValue("@empid", user["otherPager"]).DbType = DbType.String;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
}
}
cnx.Close();
}
}
static DataTable GetEnabledUsers()
{
string connectionString = "Provider=SQLOLEDB;Data Source=SRV03;Initial Catalog=DEV;Integrated Security=SSPI";
string searchRoot = "";
string activeUsersFilter = "(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
string attributes = "sAMAccountName, telephoneNumber, mail, otherPager";
string searchScope = "subtree";
string commandText = String.Format("{0};{1};{2};{3}",searchRoot, activeUsersFilter, attributes, searchScope);
DataTable enabledUsers = new DataTable();
using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(commandText, connectionString))
{
try
{
dataAdapter.Fill(enabledUsers);
}
catch (System.Exception se)
{
Console.WriteLine(se.Message);
}
}
return enabledUsers;
}
}