By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,989 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,989 IT Pros & Developers. It's quick & easy.

Need help porting code to VB.NET

P: n/a
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.

Martin

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
Dim sDirName
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
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
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
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Martin,

You need to get out of the habit of using default properties.

Change sResult = RS.Fields(0) to sResult = RS.Fields(0).Value
and you should be in business.

--
Al Reid

--
Al Reid

"Martin" <bm******@email.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Martin

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
Dim sDirName
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
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
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

Nov 21 '05 #2

P: n/a
Al,

Thanks for the quick reply and help! It works now! I cant believe it was
something that small.

-Martin

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Martin,

You need to get out of the habit of using default properties.

Change sResult = RS.Fields(0) to sResult = RS.Fields(0).Value
and you should be in business.

--
Al Reid

--
Al Reid

"Martin" <bm******@email.com> wrote in message

news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Martin

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
Dim sDirName
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
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
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


Nov 21 '05 #3

P: n/a
Martin,

No problem. It's often the small stuff that bites you.<g>

--
Al Reid

"Martin" <bm******@email.com> wrote in message news:O7**************@TK2MSFTNGP11.phx.gbl...
Al,

Thanks for the quick reply and help! It works now! I cant believe it was
something that small.

-Martin

"Al Reid" <ar*****@reidDASHhome.com> wrote in message
news:uH**************@TK2MSFTNGP11.phx.gbl...
Martin,

You need to get out of the habit of using default properties.

Change sResult = RS.Fields(0) to sResult = RS.Fields(0).Value
and you should be in business.

--
Al Reid

--
Al Reid

"Martin" <bm******@email.com> wrote in message

news:%2****************@TK2MSFTNGP12.phx.gbl...
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.

Martin

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
Dim sDirName
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
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
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



Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.