title: Adding an AD user to an AD group
body: <p>I have a VB application (.NET 4.0) where a user selects an AD group they own, then can add users from a predefined list to that group. The groups and pulled from AD and the users are pulled from Oracle but are all existing AD users.</p>
  
  <p>You will see three commented code blocks, I have tried all three and get "COMException was unhandled by user code: Unspecified error" with each one.</p>
  
  <pre><code>&lt;WebMethod()&gt; _
  Public Shared Function AddDirectReport(ByVal User As String, ByVal Group As String) As String
      Dim GroupMembers As List(Of String) = LoadGroupMembers(Group)
      If GroupMembers.Contains(User) Then
          Return "USER " &amp; User &amp; " IS ALREADY IN GROUP " &amp; Group
      End If
  
      Dim SearchRoot As New DirectoryEntry("[LDAP Path]")
  
      Dim GroupSearcher As New DirectorySearcher
      With GroupSearcher
          .SearchRoot = SearchRoot
          .Filter = "(&amp;(ObjectClass=Group)(CN=" &amp; Group &amp; "))"
      End With
  
      Dim UserSearcher As New DirectorySearcher
      With UserSearcher
          .SearchRoot = SearchRoot
          .Filter = "(&amp;(ObjectClass=Person)(CN=" &amp; User &amp; "))"
      End With
  
      Dim g As DirectoryEntry = GroupSearcher.FindOne.GetDirectoryEntry
      Dim u As DirectoryEntry = UserSearcher.FindOne.GetDirectoryEntry
  
      'With u
      '    .Properties("memberof").Add(g)
      '    .CommitChanges()
      'End With
  
      'With g
      '    .Properties("member").Add(u)
      '    .CommitChanges()
      'End With
  
      'With g
      '    .Properties("members").Add(u)
      '    .CommitChanges()
      'End With
      Return "Success?"
  End Function
  </code></pre>