title: C# Active Directory Adding a user
body: <p>I'm trying to create a user with textboxes that have to be filled in. The properties, username and password are correct yet I get a "There is no such object on the server" error.  </p>
  
  <pre><code>private void btn_AddStudent_Click(object sender, EventArgs e)
      {
          try
          {
              // Username and password.
              string UserName = UsernameGenerate(8);
              string Password = PasswordGenerate(8);
  
              // OU path.
              string ouString = "OU = " + cmb_Study.Text;
              string LDAPstring = "LDAP://" + "OU = Studies, " + ouString + ", DC=DR, DC=GUI";
              DirectoryEntry dirEntry = new DirectoryEntry(LDAPstring);
  
              // Create user.
              string userString = "CN = " + UserName;
              DirectoryEntry newUser = dirEntry.Children.Add(userString, "user");
              newUser.CommitChanges();
  
              newUser.Properties["userprincipalname"].Add(UserName + "@DR.GUI");
              newUser.Properties["samaccountname"].Value = UserName;
              newUser.Invoke("setPassword", new object[] {Password});
              //Rest of the properties
  
              newUser.Close();
              dirEntry.Close();
              newUser.Dispose();
              dirEntry.Dispose();
              MessageBox.Show("User has been succesfully added");
          }
          catch (Exception E)
          {
              MessageBox.Show("Creating user has failed. " + E.Message);
          }
      }
  </code></pre>
  
  <p>I have been trying to search for a solution for quite a while now but I'm still clueless where the problem is. Did I make a mistake in the OU path? Which for example would be OU= Studies/ISM or Studies/WEBDEV. Or did I make any mistakes in my LDAP?</p>
  
  <p>I'm fairly new to C# and programming in overal, I'm looking for a simple solution if possible. </p>