LDAP query to DataGrid

LDAP query to DataGrid

Post by Dave » Mon, 11 Jul 2005 13:40:40

Could someone fix this for me please. The last bit i cant figure out is the
last line in the sub.

Results.SetDataBinding(myTable.DefaultView, "")




Imports System.DirectoryServices

Partial Class _Default

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

Dim strADPath As String

strADPath = "domain.com"

Dim rootEntry As DirectoryEntry = New DirectoryEntry("LDAP://" & strADPath,
"Administrator", "password")

Dim searcher As New DirectorySearcher(rootEntry)




'searcher.PropertiesToLoad.AddRange(New String() {"cn", "mail"})

'would also work and saves you some code

searcher.Filter = ("(&(objectCategory=person)(objectClass=user))")

Dim myTable As New Data.DataTable("Results")

Dim colName As String

For Each colName In searcher.PropertiesToLoad

myTable.Columns.Add(colName, GetType(System.String))


Dim queryResults As SearchResultCollection

queryResults = searcher.FindAll()

Dim result As SearchResult

For Each result In queryResults

Dim dr As Data.DataRow = myTable.NewRow()

For Each colName In searcher.PropertiesToLoad

If result.Properties.Contains(colName) Then

dr(colName) = CStr(result.Properties(colName)(0))


dr(colName) = ""

End If




Results.SetDataBinding(myTable.DefaultView, "")

End Sub

End Class


LDAP query to DataGrid

Post by Dave » Tue, 12 Jul 2005 11:13:40

ust incase if anyone is interested... Seems not. ;) I got it sorted at
last. I Started from scratch..... again! With the help of numerous resources
from MSDN no less. For the any noobs out there... (I was one last week,
still am!), Drag and drop a GridView or DataGrid Control to the HTML source
page. Then just copy and past this code below into the Code Behind aspx.vb
page. Thats it your done!

Hmmm.... Q. How do I populate a Treeview Control with Users and their
A. DSML over SOAP...
This could be interesting, Any suggestions welcome...

Imports System.Data

'You need to add a Reference to this.....

Imports System.DirectoryServices

Partial Class _Default

Inherits System.Web.UI.Page

Function CreateDataSource() As ICollection

Dim dt As New DataTable()

Dim dr As DataRow

dt.Columns.Add(New DataColumn("CommonName", GetType(String)))

dt.Columns.Add(New DataColumn("Email Address", GetType(String)))

dt.Columns.Add(New DataColumn("CompanyName", GetType(String)))

dt.Columns.Add(New DataColumn("sAMAccountName", GetType(String)))

'If you want to search a particular OU dont forget the LDAP structure reads
backwards to the root.

'"LDAP://OU=Child,OU=Parent,OU=GrandParent,DC=domain,DC=com, "UserName",

Dim root As New
"UserName", "password")

Dim rootSearch As New DirectorySearcher(root)

Dim SearchResult As SearchResult

Dim results As SearchResultCollection

rootSearch.PropertiesToLoad.AddRange(New String() {"cn", "mail", "company",

rootSearch.Filter = "(&(objectCategory=Person)(objectClass=user))"

results = rootSearch.FindAll

For Each SearchResult In results


dr = dt.NewRow()

'If the property contains no value it wont be included in the search results

' So use the If..... End If statements

If SearchResult.Properties.Contains("CN") Then

dr(0) = SearchResult.Properties("CN").Item(0)

End If

If SearchResult.Properties.Contains("mail") Then

dr(1) = SearchResult.Properties("mail").Item(0)


dr(1) = "No E-Mail Address"

End If

If SearchResult.Properties.Contains("company") Then

dr(2) = SearchResult.Properties("company").Item(0)

End If

If SearchResult.Properties.Contains("sAMAccountName") Then

dr(3) = SearchResult.Properties("sAMAccountName").Item(0)

End If


Catch ex As Exception

Dim debug As String = ex.Message

End Try


Dim dv As New DataView(dt)

Return dv

End Function 'CreateDataSource

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load

If Not IsPostBack Then

' Load this data only once.

GridView1.DataSource = CreateDataSource()


End If

End Sub

End Class


"Dave" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...