Solved

Use  .PropertiesToLoad.Add("nvdPolicy")

Posted on 2010-08-12
7
511 Views
Last Modified: 2013-11-07
I have a path in active directory and need to get some values for that path with .net 2.0.  I can see these values with adsi edit, but would like to populate a combobox with them.

Here is what I have, but it's not working.  What am I missing?
  Dim strSoftType As String = ""
        If RadioButton2.Checked = True Then
            strSoftType = "OU=Software,"
        ElseIf RadioButton1.Checked = True Then
            strSoftType = "OU=Softserv,"
        End If
        Dim SoftName = "OU=" & ComboBox1.Text.Trim & ","
        Dim strTypeInstall As String = "CN=" & ComboBox2.Text.Trim & ","
        ComboBox3.Items.Clear()

        Dim strSoftwareLoc, strSoftwareName As DirectoryEntry
       


        Dim strResult As SearchResult
        Dim strSoftwareList As String = ""
        strSoftwareLoc = New DirectoryEntry("LDAP://" & strTypeInstall & SoftName & strSoftType & "OU=Radia,OU=Applications,DC=mydomain,DC=com")
        Dim strSearch As New DirectorySearcher(strSoftwareLoc)

        With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.

            Dim title As DirectoryEntry
            title = strResult.GetDirectoryEntry.Properties("nvdPolicy").Value '***************** I FAIL HERE




            ComboBox3.Items.Add(title)    'add to combobox and remove OU=

        Next
0
Comment
Question by:chadmanvb
  • 4
  • 3
7 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33429361
for better practice u first want to check if property is exists, for instance:

    Dim title As DirectoryEntry
dim objVal  as object
objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")
if not objVal  = nothing then
            title = objVal.Value
else
'property nvdPolicy doesn't exists
end if
0
 

Author Comment

by:chadmanvb
ID: 33429809
I get this error:
Operator '=' is not defined for type 'PropertyValueCollection' and 'Nothing'.

on line: if not objVal  = nothing then

any ideas?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 33429898
try debug your code to till this line:
objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")

what is the value of objVal?
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 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 33430026
try the following:

With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.FindAll()
            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicy")

            Next
 For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicyOverride")

            Next
        Next
0
 

Author Comment

by:chadmanvb
ID: 33430550
Looks like I am still getting the same error I originally had:

Unable to cast object of type 'System.String' to type 'System.DirectoryServices.DirectoryEntry'.

 With strSearch
            .SearchScope = SearchScope.Subtree
            .PropertiesToLoad.Add("nvdPolicy")
            .PropertiesToLoad.Add("nvdPolicyOverride")
        End With

        strSearch.SearchRoot = strSoftwareLoc

        For Each strResult In strSearch.FindAll()
            Dim title As DirectoryEntry
            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicy")


                Dim objVal As Object
                objVal = strResult.GetDirectoryEntry.Properties("nvdPolicy")

                title = objVal.Value  '*******************fails here


            Next

            For Each item In strResult.GetDirectoryEntry().Properties("nvdPolicyOverride")


            Next
            ComboBox3.Items.Add(title)    'add to combobox and remove OU=
        Next
0
 

Author Comment

by:chadmanvb
ID: 33431171
When I change the variable to Dim strAttributeValue As Object

It works fine.  Can a directory value not be converted to a string?
0
 

Author Closing Comment

by:chadmanvb
ID: 33435944
Thanks, that's working.
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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

20 Experts available now in Live!

Get 1:1 Help Now