Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help porting code to VB.NET

Status
Not open for further replies.

bmquiroz

IS-IT--Management
Sep 26, 2003
207
US
Hi all,

I have some VBScript code that I am trying to port over to VB.NET. I am receiving an error "Cast from type 'Field' to type 'String' is not valid." when calling the MapLegacyExchangeDN method. I'm thinking that the parameter
sDirName isn't being properly passed to the function. The same code works in VBScript. Can someone help, this should be an easy one but I cant figure it out! And yes, I am a VB.NET newbie!

Thanks.

Code:

Dim sLDAPServer As String = "server.domain.local"
Dim sDirName As String = "/o=DOMAIN/ou=First Administrative
Group/cn=Recipients/cn=shmoej"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objSession
Dim objFolder
Dim pubContactItems
Dim sEmailName

sDirName = MapLegacyExchangeDN(sDirName)
sDirName = RetrieveSMTP(sDirName)
sEmailName = sDirName

MsgBox("E-mail: " & sEmailName)

Error_Handler:
If Err.Number <> 0 Then
MsgBox("Error number: " & (Err.Number) & ", " & Err.Description)
Err.Clear()
Else : MsgBox("Everything OK")
End If

End Sub

Private Function RetrieveSMTP(ByVal sDirName As String)

Dim objMailbox As Object
Dim sMemberAddr

objMailbox = GetObject("LDAP://" & sLDAPServer & "/" & sDirName)
If Err.Number = 0 Then
sMemberAddr = objMailbox.GetEx("mail")(0)
If Err.Number = 0 Then
End If
End If

RetrieveSMTP = sMemberAddr

End Function

Private Function MapLegacyExchangeDN(ByVal sDirName As String)

Dim ADOconn
Dim strADOQueryString
Dim RS
Dim sResult
Dim bShowAll

ADOconn = CreateObject("ADODB.Connection")
ADOconn.Provider = "ADSDSOObject"
ADOconn.Open("Active Directory Provider")

strADOQueryString = "<LDAP://" & sLDAPServer & ">;(legacyExchangeDN=*" & sDirName & "*);distinguishedName;subtree"
On Error Resume Next
RS = ADOconn.Execute(strADOQueryString)
If Err.Number <> 0 Then
MsgBox("Error searching for legacyExchangeDN " & Err.Number & " " & Err.Description)
End If
sResult = ""
If Not RS.EOF Then
If RS.recordcount > 1 Then
bShowAll = True
Else
bShowAll = False
End If

While Not RS.EOF
sResult = RS.Fields(0)
RS.MoveNext()
End While
End If

RS.Close()
RS = Nothing
ADOconn = Nothing

MapLegacyExchangeDN = sResult

End Function

 
Nevermind.

Changed sResult = RS.Fields(0) to sResult = RS.Fields(0).Value and it started working!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top