获取对象引用未设置为对象错误的实例

时间:2016-10-13 14:21:14

标签: c#

所以我正在努力构建一个小型的webapp,它只会收集有关用户的信息并通过电子邮件发送。我坚持从活动目录收集数据。我收到一个错误,指出对象引用未设置为对象的实例。奇怪的是这个错误只发生在生产环境中,而不是在开发中。我已经调试过,确定该对象返回null但我不明白为什么这个在dev中工作但不是prod。代码块在......

之下

似乎失败了     org = sresult.Properties [" company"] [0] .ToString()

   //populate username
    username = Environment.UserName;
    //get active directory information
    string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
    //create instance for directory entry
    DirectoryEntry de = new DirectoryEntry(connection);
    //create instance of the directory searcher
    DirectorySearcher desearch = new DirectorySearcher(de);
    //set the search filter
    desearch.Filter = "(sAMAccountName=" + username + ")";
    //find the first instance
    SearchResult sresult = desearch.FindOne();


    // get Organization or Company of User
    org = sresult.Properties["company"][0].ToString();
    //get name of individual (get first name and last name and join them)
    firstname = sresult.Properties["givenName"][0].ToString();
    lastname = sresult.Properties["sn"][0].ToString();
    name1 = firstname + " " + lastname;
    //get location or Department of user
    location = sresult.Properties["department"][0].ToString();
    //get title
    title = sresult.Properties["title"][0].ToString();
    //get phone
    phone = sresult.Properties["telephoneNumber"][0].ToString();
    //get email
    email = sresult.Properties["mail"][0].ToString();

0 个答案:

没有答案