473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

LDAP query to DataGrid

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, "")

Thanks

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~

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.Add("cn")

searcher.PropertiesToLoad.Add("email")

searcher.PropertiesToLoad.Add("adsPath")

'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))

Next

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))

Else

dr(colName) = ""

End If

Next

myTable.Rows.Add(dr)

Next

Results.SetDataBinding(myTable.DefaultView, "")

End Sub

End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nov 19 '05 #1
1 3154
Just 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
'managers'.
A. DSML over SOAP...
This could be interesting, Any suggestions welcome...
Dave

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
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",
"password")

Dim root As New
DirectoryServices.DirectoryEntry("LDAP://OU=User,DC=domain,DC=com",
"UserName", "password")

Dim rootSearch As New DirectorySearcher(root)

Dim SearchResult As SearchResult

Dim results As SearchResultCollection

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

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

results = rootSearch.FindAll

For Each SearchResult In results

Try

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)

Else

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

dt.Rows.Add(dr)

Catch ex As Exception

Dim debug As String = ex.Message

End Try

Next

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()

GridView1.DataBind()

End If

End Sub

End Class

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
"Dave" <dd*@ddd.ie> wrote in message
news:DO********************@giganews.com...
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, "")

Thanks

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~

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.Add("cn")

searcher.PropertiesToLoad.Add("email")

searcher.PropertiesToLoad.Add("adsPath")

'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))

Next

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))

Else

dr(colName) = ""

End If

Next

myTable.Rows.Add(dr)

Next

Results.SetDataBinding(myTable.DefaultView, "")

End Sub

End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jeremy | last post by:
Hello. I have an asp.net application that resides on a non-DC / BDC Sharepoint Server (although it is logged into the domain). The application will perform lookups based on the current user...
4
by: m96 | last post by:
hi, i'm trying to make a query to a ldap server (version v2 or v3 doen't matter) with c#. the query works just fine but the problem is that i can't read the custom attributes/fields, since .net...
3
by: Jay | last post by:
I have a simple LDAP query (grabs all users from a particular AD group and populates a checkboxlist) that works perfectly fine on the development machine logged on locally as any user. When I...
1
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem...
0
by: Dave | last post by:
Could some kind soul please explain to me how i could implement this in a DataGrid. I found the sample here. http://www.wwwcoder.com/main/parentid/272/site/1694/68/default.aspx I'm a complete...
3
by: Chad Beckner | last post by:
I am starting to translate some code from ASP to ASP.NET (VB). I was able to query ADS to get a users groups that they belong to, and also query a group and get a list of users. However, I can't...
5
by: Bryan | last post by:
Hello, I have a asp.net app working with directory services on my Windows XP development machine. However when I moved the application over to our production server (Win 2000 Server) it no longer...
0
by: Gerry Sinkiewicz | last post by:
Looking for a bit of software that will accept an LDAP query via port 389 and convert it to an SQL query and return the results to the originating LDAP client. Basically a dialect converter LDAP...
3
by: RJN | last post by:
Hi I've written a code that queries Windows LDAP server and works fine, but the same doesn't work when querying Solaris LDAP server. DirectoryEntry de = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.