Solved

Use  .PropertiesToLoad.Add("nvdPolicy")

Posted on 2010-08-12
7
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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