Solved

Use  .PropertiesToLoad.Add("nvdPolicy")

Posted on 2010-08-12
7
523 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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