Solved

asp.net DirectoryEntry error

Posted on 2014-11-23
9
276 Views
Last Modified: 2014-11-25
I'm trying bind the entry from the first dropdownlist (AD domain selection) to populate a second dropdownlist with a list of user 'cn'.

The complier error message: BC30311: Value of Type 'String' cannot be converted to 'System.DirectoryServices.DirectoryEntry'

Line 51: Dim adEntry As DirectoryEntry = adStrng
Visual studio also flags that 'dropdownlist1' is not declared within Public Function EumerateDomainUsers()

Imports System
Imports System.Data
Imports System.Linq
Imports System.IO
Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices.ActiveDirectory
Imports System.Security
Imports System.Security.Permissions
Imports System.Text
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Public Function EnumerateDomains() As ArrayList
        Dim alDomains As New ArrayList()
        Dim currentForrest As Forest = Forest.GetCurrentForest()
        Dim myDomains As DomainCollection = currentForrest.Domains
        For Each objDomain As Domain In myDomains
            alDomains.Add(objDomain.Name)
        Next
        Return alDomains
    End Function
 
    Protected Sub Selection_Changed(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles Me.Load
        DropDownList1.Items.Clear()
        DropDownList1.AppendDataBoundItems = True
        DropDownList1.SelectedIndex = 0
        DropDownList1.DataSource = EnumerateDomains()
        DropDownList1.DataBind()
        'Dim dUsers As String = DropDownList1.SelectedItem.ToString()
    End Sub
 
    Public Function EumerateDomainUsers() As ArrayList
        Dim domainUsers As New ArrayList()
        Dim usr As String
        'Dim fqdns As String = DropDownList1.SelectedItem.ToString()
        Dim fqdns As String = DropDownList1.SelectedItem.Text
        Dim adStrng As String = "LDAP://" & fqdns
        Dim adEntry As DirectoryEntry = (adStrng)
        Dim searcher As DirectorySearcher = New DirectorySearcher(adEntry)
        searcher.Sort.PropertyName = "cn"
        Dim results As SearchResultCollection
        Dim result As SearchResult
        searcher.PropertiesToLoad.Add("cn")
        results = searcher.FindAll
        For Each result In results
            usr = result.GetDirectoryEntry().Properties("cn").Value
            domainUsers.Add(usr)
        Next
        Return domainUsers
    End Function
 
    Protected Sub Selection_Changed1(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.Load
        DropDownList2.Items.Clear()
        DropDownList2.AppendDataBoundItems = True
        DropDownList2.SelectedIndex = 0
        DropDownList2.DataSource = EumerateDomainUsers()
        DropDownList2.DataBind()
        'Dim dUsers As String = DropDownList1.SelectedItem.ToString()
    End Sub
End Class

Open in new window

0
Comment
Question by:patelbg2001
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 40460748
Try changing this line from:
49: Dim adEntry As DirectoryEntry = (adStrng)
to
49: Dim adEntry As DirectoryEntry = GetObject(adStrng)
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 40460762
The change returned the following error. the page began to load, is it better to getobject as a separate function?

Unable to cast COM object of type 'System.__ComObject' to class type 'System.DirectoryServices.DirectoryEntry'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about the error and where it originated in the code. 



Exception Details: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'System.DirectoryServices.DirectoryEntry'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
0
 
LVL 19

Accepted Solution

by:
Peter Hutchison earned 500 total points
ID: 40460794
Oops, I was thinking of ActiveDS.IADsUser objects, try this syntax:

49:  Dim adEntry As DirectoryEntry = New System.DirectoryServices.DirectoryEntry(adStrng)
0
 
LVL 1

Expert Comment

by:Steve813
ID: 40461057
I'm a C# guy so I hope I get the VB.Net syntax right here...

Line 57 should be changed.  You already have the "cn" in the SearchResult variable of "result" and this can be accessed via the Properties.  Change line 57 to:

usr = result.Properties("cn").Value
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 6

Author Comment

by:patelbg2001
ID: 40461164
I try suggested in the morning.
Is c# more flexible for coding directory services in asp.net than vb?
0
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 40461579
In C#: the syntax would be similar:

DirectoryEntry adEntry;
adEntry = new DirectoryEntry(adStrng);
or
adEntry = new System.DirectoryServices.DirectoryEntry(adStrng);
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 40461781
it's now picked up another error

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 47:         Dim usr As String
Line 48:         'Dim fqdns As String = DropDownList1.SelectedItem.ToString()
Line 49:         Dim fqdns As String = DropDownList1.SelectedItem.Text
Line 50:         Dim adStrng As String = "LDAP://" & fqdns
Line 51:         'Dim adEntry As DirectoryEntry = GetObject(adStrng)

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
   _Default.EumerateDomainUsers() in D:\ADConnect\Default.aspx.vb:49
   _Default.Selection_Changed1(Object sender, EventArgs e) in D:\ADConnect\Default.aspx.vb:71
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Control.LoadRecursive() +70
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3177
0
 
LVL 19

Expert Comment

by:Peter Hutchison
ID: 40461953
Looks like you missed the 'new' statement for line 51 to create a new instance of object type DirectoryEntry.

Dim adEntry As DirectoryEntry = New DirectoryEntry(adStrng)
0
 
LVL 6

Author Comment

by:patelbg2001
ID: 40463459
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

The error was at line 49
Line 49:         Dim fqdns As String = DropDownList1.SelectedItem.Text

Line51:  was commented out & ignored.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Name space syntax error 12 43
Resolve Dependency Issues 4 47
Help with C#, MVC, razor. 6 23
Run a batch file when a Crystal Report is opened 5 15
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now