Problem with asp.net function syntax (not returning values correctly)

Posted by Phil on Stack Overflow See other posts from Stack Overflow or by Phil
Published on 2010-04-01T08:29:50Z Indexed on 2010/04/01 8:33 UTC
Read the original article Hit count: 533

Filed under:
|
|
|

I have an active directory search function:

    Function GetAdInfo(ByVal ADDN As String, ByVal ADCommonName As String, ByVal ADGivenName As String, ByVal ADStaffNum As String, ByVal ADEmail As String, ByVal ADDescription As String, ByVal ADTelephone As String, ByVal ADOffice As String, ByVal ADEmployeeID As String) As String
        Dim netBIOSname As String = Me.Request.LogonUserIdentity.Name
        Dim sAMAccountName As String = netBIOSname.Substring(netBIOSname.LastIndexOf("\"c) + 1)
        Dim defaultNamingContext As String
        Using rootDSE As DirectoryServices.DirectoryEntry = New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            defaultNamingContext = rootDSE.Properties("defaultNamingContext").Value.ToString()
        End Using
        Using searchRoot As DirectoryServices.DirectoryEntry = _
                            New DirectoryServices.DirectoryEntry("LDAP://" + defaultNamingContext, _
                                "kingkong", "kingkong", DirectoryServices.AuthenticationTypes.Secure)
            Using ds As DirectoryServices.DirectorySearcher = New DirectoryServices.DirectorySearcher(searchRoot)
                ds.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", sAMAccountName)
                Dim sr As DirectoryServices.SearchResult = ds.FindOne()

                'If sr.Properties("displayName").Count = 0 Then whatever = string.empty '' (how to check nulls when required)
                ' End If
                ADDN = (sr.Properties("displayName")(0).ToString())
                ADCommonName = (sr.Properties("cn")(0).ToString())
                ADGivenName = (sr.Properties("givenname")(0).ToString())
                ADStaffNum = (sr.Properties("sn")(0).ToString())
                ADEmail = (sr.Properties("mail")(0).ToString())
                ADDescription = (sr.Properties("description")(0).ToString())
                ADTelephone = (sr.Properties("telephonenumber")(0).ToString())
                ADOffice = (sr.Properties("physicalDeliveryOfficeName")(0).ToString())
                '  ADEmployeeID = (sr.Properties("employeeID")(0).ToString())
            End Using
        End Using
        Return ADDN
        Return ADCommonName
        Return ADGivenName
        Return ADStaffNum
        Return ADEmail
        Return ADDescription
        Return ADTelephone
        Return ADOffice
        ' Return ADEmployeeID
    'have commented out employee id as i dont have one so it is throwing null errors.
    ' im not ready to put labels on the frontend or catch this info yet
End Function

The function appears to work, as when I put a breakpoint at the end, the variables such as ADDN do have the correct values.

Then I call the function in my page_load like this:

GetAdInfo(ADDN, ADCommonName, ADGivenName, ADStaffnum, ADEmail, ADDescription, ADTelephone, ADOffice, ADEmployeeID)

Then I try to response.write out one of the vars to test like this:

Response.Write(ADDN) 

But the value is empty.

Please can someone tell me what I am doing wrong. Thanks

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about function