简体   繁体   中英

Not able to bind DataGridView with Recordset

I am using below code to query Active Directory and get list of users. The code is working in VB Macro. I modified a bit syntactically to make it work on VB.NET (VS 2022).

The RecordCount is appearing as 900 but I am not able to bind it to DataGridView. Not enough knowledge of VB. I referred to samples which use OleDB DataAdapter but in the code below the command object is different so it throws error.

Also, this line throws error. It is working in Excel Macro.

oCommand1.Properties("SearchScope") = 2

Please advise how to display records in Grid:

'Open the connection.
'This is the ADSI OLE-DB provider name
oConnection1.Provider = "ADsDSOObject"
oConnection1.Open("Active Directory Provider")

oCommand1.ActiveConnection = oConnection1

strQuery = "select c, l, SAMAccountName,displayName, distinguishedName, cn, sn,givenName,title,mail, department, manager, userAccountControl " &
" from 'GC://dc=TestAD,dc=net'" &
"WHERE objectCategory='Person'" &
"AND objectClass='user'"

oCommand1.CommandText = strQuery
oCommand1.Properties("SearchScope") = 2

rs = oCommand1.Execute()
lblRecords.Text = rs.RecordCount
DataGridView1.DataSource = rs

oConnection1.Close()

Use Directory services and LDAP query:

Imports System.DirectoryServices

{......}
    
    Dim oD As DirectoryEntry
    Dim oS As DirectorySearcher
                
    oD = New DirectoryEntry("LDAP://RootDSE")
    oS = New DirectorySearcher(oD)
            
    oS.Filter = "(&(objectClass=user))"
    oS.SearchScope = SearchScope.Subtree
    
    ''''''''''''''''''''''
    'by default all objects property will be retrieve
    'if you just need somme of them use
    'oS.PropertiesToLoad.Add("SAMAccountName")
    'oS.PropertiesToLoad.Add("displayName")
    'oS.PropertiesToLoad.Add("CN")
    'etc...
    'that will speed up search
    ''''''''''''''''''''''
    
    Dim src As SearchResultCollection = oS.FindAll()

    oD.Close()
    oS.Dispose()

    Dim dt As New DataTable()
    dt.Columns.Add("samaccountname")
    dt.Columns.Add("givenName")
    dt.Columns.Add("sn")
    dt.Columns.Add("mail")
    ' add here others columns needed in dt
    
    For Each sr As SearchResult In src
        Dim dr As DataRow = dt.NewRow()
        Dim de As DirectoryEntry = sr.GetDirectoryEntry()
        dr("samaccountname") = de.Properties("samaccountname").Value.ToString()
        dr("givenName") = de.Properties("givenName").Value.ToString()
        dr("sn") = de.Properties("sn").Value.ToString()
        dr("mail") = de.Properties("mail").Value.ToString()
        'etc... fill others dt columns based on propertys needed
        dt.Rows.Add(dr)
        de.Close()
    Next sr

    lblRecords.Text = dt.rows.count-1
    DataGridView1.DataSource = dt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM