将ComboBox选定值设置为AD查询

时间:2014-11-26 10:48:04

标签: vb.net combobox active-directory directoryservices

我正在运行AD查询以从用户个人资料中提取所选属性。我正在选择extensionAttribute3,4,5,6,7和& 8.虽然我可以将结果显示为文本,但我想将组合框的选定值设置为结果。

所以扩展属性3,5& 7 =安全问题,4,6和6 8是答案。我有3个组合框,每个组合框都有一个用户可以选择的15个可能的安全问题列表,然后提供答案。我有我的脚本来更新AD的问题&选择答案。但是,当我再次运行应用程序时,我想从extensionAttribute 3,5和& 7,设置为默认选择的foreach组合框。

当前的AD查询代码:

Private Function GetUserProperties()
    Dim ADName As String = GetLogonName()
    Dim CurrentPIN As String = Nothing
    Dim bSuccess As Boolean = False
    Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
    Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
    Dim Q1Value As String = Nothing
    dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute4")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute5")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute6")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute7")
    dirSearcher.PropertiesToLoad.Add("extensionAttribute8")
    dirSearcher.SearchScope = SearchScope.Subtree
    Try
        Dim dirResult As SearchResult = dirSearcher.FindOne()
        bSuccess = Not (dirResult Is Nothing)
        If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value Is Nothing Then
            Return "<not set>"
        Else
            Q1Value = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
            Q1ComboBox.SelectedIndex = Q1Value
        End If
    Catch ex As Exception
        bSuccess = False
        MsgBox("No Connection to the domain." & Environment.NewLine & "Please connect to corporate network & try again.", MsgBoxStyle.Critical, "Network Error")
        Application.Exit()
    End Try
    Return False
End Function

1 个答案:

答案 0 :(得分:0)

在评论中格式化代码真的很难,我把它们放在这里 我不是VB程序员,可能有语法错误。

您没有为extensionAttribute4-8提供代码,因此很难找到它们的错误。你的意思是extensionAttribute4-8,只是重复try-catch中的if-else块不起作用? 例如,您无法获得下面的extensionAttribute4的值?

' code for extensionAttribute3, omitted here
....

' code for extensionAttribute4
If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute4").Value Is Nothing Then
    Return "<not set>"
Else
    A1Value = dirResult.GetDirectoryEntry.Properties("extensionAttribute4").Value.ToString
    A1ComboBox.SelectedIndex = A1Value
End If

' repeat for extensionAttribute5-8
....

对于使用已在SearchResult中加载的属性,您已经通过调用ToString来处理转换为字符串问题(在注释中提到)。你可以做同样的事情。但是,您应该检查dirResult.GetDirectoryEntry.Properties("...").Value Is Nothing

,而不是检查dirResult.Properties("...").Count > 0
    Dim dirResult As SearchResult = dirSearcher.FindOne()
    bSuccess = Not (dirResult Is Nothing)
    If dirResult Is Nothing OrElse dirResult.Properties("extensionAttribute3").Count <= 0 Then
        Return "<not set>"
    Else
        Q1Value = dirResult.Properties("extensionAttribute3")[0].ToString
        Q1ComboBox.SelectedIndex = Q1Value
    End If