Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Our organization has a BES server that I have written a code to find the users according their exchange email address and now I want to relocate them through their exchange server name (Instances) using C#.net. I have a table with those server names in my sql server database too but I don't know how I should handle it. I have started but I don't know how I should continue. Would you please help me to find out where is my mistake? My searchAd code is this:

private String SearchAd(string ID)

{
    DirectoryEntry ldapObj;
    DirectorySearcher searchObj;
    SearchResult entryObj;
    DirectoryEntry entry;
    string strValue = "No Entry Found";
    string strSearchFor;

    ldapObj = new DirectoryEntry();
    ldapObj.Username = @"XYZ";
    ldapObj.Password =System.Configuration.ConfigurationManager.AppSettings["XXX"].Trim();
    ldapObj.Path = "LDAP://XYZ/dc=XX,dc=XX,dc=XX";
    ldapObj.AuthenticationType = AuthenticationTypes.Secure;

    searchObj = new DirectorySearcher(ldapObj);
    searchObj.Filter = "(samaccountname=" + ID + ")";
    searchObj.ReferralChasing = ReferralChasingOption.All;
    searchObj.PropertiesToLoad.Add("homemdb");

     try
    {
        entryObj = searchObj.FindOne();

        try
        {
            // give me the homemdb attribute
            entry = entryObj.GetDirectoryEntry();
            ResultPropertyCollection attributes;
            attributes = entryObj.Properties;
            strSearchFor = "homemdb";
            strValue = Convert.ToString(attributes[strSearchFor][0]); //assuming only one homemdb value, could be an invalid assumption
        }
        catch 
        {
           // Not really an error, but there was no entry found with this filter value
            strValue = "No entry found";
        }

    }
    catch(Exception srhex)          
    {
        Trace.Write("info", "Error occured. Error is:" + srhex.ToString());
        writeLog("Active Direcory read exception: " + srhex.ToString());
        strValue = "Error in searching Active Directory.";
    }

    searchObj.Dispose();
    entryObj = null;
    ldapObj.Close();
    ldapObj.Dispose();
    searchObj = null;
    ldapObj = null;

    return strValue;
}


}

private String Findbest(String ID)
{
    string strLocation;
    string[] ExchangeInfo;

    strLocation = SearchAD(ID.Trim());

    if (strLocation == "No entry found")
    {
        MobileSolutions.InfoBox(this, "No Exchange mailbox was found for this account. An Exchange e-mail account is required for BlackBerry service. Please visit @@@@ to request an Exchange e-mail account.");
        return "Not Found";
    }
    else
    {
        //Trim the exchange server name to just the value between the parenteses in the RDN.
        ExchangeInfo = strLocation.Split('(');
        ExchangeInfo = ExchangeInfo[1].Split(')');
        Trace.Write("info", "Exchange server found is: " + ExchangeInfo[0]);

        SqlClient.SqlDataReader SQLQuery;
        SqlConnection.DBName = "XXXXX";
        SQLQuery = SQLCon.Connect("select BESInstance from ExchangeMap where Exchange = '" = ExchangeInfo[0] + "'");

        if (SQLQuery.Read() == true)
        {
            return SQLQuery.GetValue[0];
        }
        else
        {

            SendMail.Send("XXX","YYY", " website error", "A user (" + ID + ") tried to add an account, but no BES Instance was found for this users Exchange server: " + ExchangeInfo[0] + ". Please add the corresponding BES Instance for this Exchange server in the ExchangeMap on the  website.");
            return SQLQuery.GetValue[0];
        }

    }
    return "Not Found";         
}
share|improve this question
    
I don't understand your question, but it might be easier to do your search on the homeMTA attribute. –  joeqwerty Nov 2 '12 at 18:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.