VB.NET DirectorySearcher Won't retrieve AD users with empty properties -
i using code retrieve user ad datagridview on windows form. works long none of properties empty.
private sub button9_click(sender system.object, e system.eventargs) handles button9.click dim direntry system.directoryservices.directoryentry dim mysearcher system.directoryservices.directorysearcher dim domainname string = system.net.networkinformation.ipglobalproperties.getipglobalproperties().domainname try direntry = new system.directoryservices.directoryentry("ldap://" & environment.userdomainname) mysearcher = new system.directoryservices.directorysearcher(direntry) mysearcher.filter = "(samaccountname=" & textbox8.text & ")" mysearcher.propertiestoload.add("samaccountname") mysearcher.propertiestoload.add("displayname") mysearcher.propertiestoload.add("description") mysearcher.propertiestoload.add("mail") mysearcher.propertiestoload.add("profilepath") mysearcher.propertiestoload.add("homedirectory") mysearcher.propertiestoload.add("homedrive") mysearcher.propertiestoload.add("givenname") mysearcher.propertiestoload.add("sn") dim sr searchresult = mysearcher.findone() if sr nothing 'return false if user isn't found 'msgbox("cannot find") end if dim de system.directoryservices.directoryentry = sr.getdirectoryentry() dim sam string = de.properties("samaccountname").item(0).tostring if sam = "" sam = " - " end if dim displayname string = de.properties("displayname").item(0).tostring if displayname = "" displayname = " - " end if dim description string = de.properties("description").item(0).tostring if description = "" description = " - " end if dim email string = de.properties("mail").item(0).tostring if email = "" email = " - " end if dim profile string = de.properties("profilepath").item(0).tostring if profile = "" profile = " - " end if dim homedir string = de.properties("homedirectory").item(0).tostring if homedir = "" homedir = " - " end if dim homedrv string = de.properties("homedrive").item(0).tostring if homedrv = "" homedrv = " - " end if dim givenname string = de.properties("givenname").item(0).tostring if givenname = "" givenname = " - " end if dim sn string = de.properties("sn").item(0).tostring if sn = "" sn = " - " end if 'datagridview3.rows.add(de.properties("samaccountname").value.tostring, de.properties("givenname").value.tostring & " " & de.properties("sn").value.tostring, de.properties("profilepath").value.tostring, de.properties("homedirectory").value.tostring, de.properties("homedrive").value.tostring, "", "", "", "", "") datagridview3.allowusertoaddrows = true datagridview3.rows.add(sam, displayname, description, email, profile, "", homedir, "", homedrv, "") datagridview3.allowusertoaddrows = false catch end try direntry.dispose() mysearcher.dispose() end sub
the parts of code use:
if sam = "" sam = " - " end if
were attempt combat doesnt work. if find user in ad , fill in blank fields user retrieved.
please can me identify cause , how might able resolve it.
for information...
i using visual studio 2010
.net 3.5 +
many thanks,
aaron
- as know on ad samaccountname cannot null or empty.
- when attribute not set null, not "".
safety may check both "" , null.
Comments
Post a Comment