Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

XML
"<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"

C#
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";
        string connectionString = "Provider=SQLOLEDB;Data Source=SRV03;Initial Catalog=DEV;Integrated Security=SSPI";
        string searchRoot = "";//Altered here based on my AD conncetion
        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;
    }
}
Posted
Updated 17-Mar-11 22:26pm
v4

1 solution

I can't help but think this is related to you other problem: 'ADSDSOObject' failed with no error message available[^]

Have you cut'n'pasted this code from somewhere without altering it for your system as well?

If not, then edit your question and try to increase the fragment to show exactly the code that is giving the error, with a few lines either side to give context.
 
Share this answer
 
Comments
shan1395 17-Mar-11 21:31pm    
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";
string connectionString = "Provider=SQLOLEDB;Data Source=SRV03;Initial Catalog=DEV;Integrated Security=SSPI";
string searchRoot = "<ldap: dc="yourdomain,DC=com">";//Altered here based on my AD conncetion
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;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900