0

I set up three models. I'm not sure if I did it the best way, but it's the way I could understand it easily. I invoke the DepartmentProfile class. The first user found is not a manager so it goes into the else statement and successfully fills in the AD_UserProfile and adds it to the DepartmentProfile class.

The second user is the department manager so it goes into the if statement and errors out on the very first line with object not set to instance of object. What am I missing? Did I not set up the models correctly?

When it errors out

public class AD_UserProfile
    {
        public string distinguishedName { get; set; }
        public string email { get; set; }
        public string manager { get; set; }
        public string name { get; set; }
        public string userPrincipalName { get; set; } // Useful if we need to allow a user to 'log on' as a different user.  
        public string userName { get; set; } 
    }

public class AD_ManagerProfile
    {
        public string distinguishedName { get; set; }
        public string email { get; set; }
        public string manager { get; set; }
        public string name { get; set; }
        public string userPrincipalName { get; set; } // Useful if we need to allow a user to 'log on' as a different user.  
        public string userName { get; set; }
    }

public class AD_DepartmentProfile
    {
        public AD_DepartmentProfile()
        {
            this.AD_UserProfile = new HashSet<AD_UserProfile>();
        }

        public string name { get; set; }

        public virtual AD_ManagerProfile AD_ManagerProfile { get; set; }
        public virtual ICollection<AD_UserProfile> AD_UserProfile { get; set; }
    }

Here is the invocation of the classes:

public void GetDepartmentInfo(string department, string owner = "jeremy")
        {
            DirectoryEntry de = new DirectoryEntry("LDAP://server.server.com");

            DirectorySearcher ds = new DirectorySearcher(de);

            ds.Filter = ("(&(objectCategory=person)(objectClass=User)(department=" + department + "))");
            ds.SearchScope = SearchScope.Subtree;

            AD_DepartmentProfile dp = new AD_DepartmentProfile();

            dp.name = department; // assign department name

            foreach (SearchResult temp in ds.FindAll())
            {
                if (owner == temp.Properties["sAMAccountName"][0].ToString())
                {
                    //Current user is manager of department

                    dp.AD_ManagerProfile.distinguishedName = temp.Properties["distinguishedName"][0].ToString();  // This line errors out with instance not set to object error.
                    dp.AD_ManagerProfile.email = temp.Properties["mail"][0].ToString();
                    dp.AD_ManagerProfile.manager = temp.Properties["manager"][0].ToString();
                    dp.AD_ManagerProfile.name = temp.Properties["name"][0].ToString();
                    dp.AD_ManagerProfile.userPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                    dp.AD_ManagerProfile.userName = temp.Properties["sAMAccountName"][0].ToString();
                }
                else
                {
                    //Current user is in department and does not manage it

                    AD_UserProfile p = new AD_UserProfile();

                    p.distinguishedName = temp.Properties["distinguishedName"][0].ToString();
                    p.email = temp.Properties["mail"][0].ToString();
                    p.manager = temp.Properties["manager"][0].ToString();
                    p.name = temp.Properties["name"][0].ToString();
                    p.userPrincipalName = temp.Properties["userPrincipalName"][0].ToString();
                    p.userName = temp.Properties["sAMAccountName"][0].ToString();

                    dp.AD_UserProfile.Add(p);
                }
            }
        }
1

1 Answer 1

2

I don't see anywhere that you're initializing dp.AD_ManagerProfile, so it is likely null. You could either give it a value in GetDepartmentInfo or in the constructor.

if (owner == temp.Properties["sAMAccountName"][0].ToString())
{
    //Current user is manager of department

    dp.AD_ManagerProfile = new AD_ManagerProfile();

    dp.AD_ManagerProfile.distinguishedName = temp.Properties["distinguishedName"][0].ToString();  // This line errors out with instance not set to object error.

or

public AD_DepartmentProfile()
{
    this.AD_UserProfile = new HashSet<AD_UserProfile>();
    this.AD_ManagerProfile = new AD_ManagerProfile();
}
Sign up to request clarification or add additional context in comments.

5 Comments

That must be my problem, how do I properly initialize it? It is a class that is exactly the same as AD_UserProfile except I only have one manager and many users.
You beat me to it, thank you I will try that and report back.
That was my problem, thank you. I'm still new to c# so thanks for explaining it and showing example. :)
Since you're new I'll also point out that I don't see a need to have AD_DepartmentProfile and AD_UserProfile be separate classes since they have the exact same fields and no differentiating behavior. At worst there could be a base class that contains the fields and then two subclasses that could have different behavior.
Yeah I thought the same thing, but I wasn't sure how to combine them. On my main class would I just call public virtual AD_UserProfile manager_profile { get; set; } and then public virtual ICollection<AD_UserProfile> user_profile { get; set }?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.