Covert this vbs to .net

Posted on 2014-03-11
Medium Priority
Last Modified: 2014-03-11
I'm trying to convert this from vbs to .net and not haveing much luck.

dim objRecordSet
dim objConnection
dim objCommand
dim user
dim strUser
dim strUserList
Set objFSO = CreateObject("Scripting.FileSystemObject")

'======== Get User ID 
user= InputBox("Enter the ID you want to check","Enter User ID")

'======== Set up ADODB Connection 
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "LDAP Provider;"
Set objCommand =   CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection

'======== Query LDAP
Titleinfo = getTitle(user)

'======== Function to query LDAP and get the title info.
Function getTitle (userid)
 objCommand.CommandText =  "SELECT TITLE FROM 'LDAP://ldapapi/ou=people,o=my domain' WHERE unixid='" & userid & "'"
 Set objRecordSet = objCommand.Execute
 On Error Resume Next
 While Not objRecordSet.EOF
   For Each i in objRecordSet.fields
      For Each v in i.value

        WScript.Echo(v)	'the info I need
End Function


Open in new window

Question by:chadmanvb
  • 2
  • 2
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 39920601
If you want to keep all of the late binding stuff, then a quick-and-dirty conversion could be:

Module Module1

    Sub Main()
        Dim user
        Dim strUser
        Dim strUserList
        Dim objFSO = CreateObject("Scripting.FileSystemObject")

        '======== Get User ID 
        user = InputBox("Enter the ID you want to check", "Enter User ID")

        '======== Query LDAP
        Dim Titleinfo = getTitle(user)
    End Sub

    '======== Function to query LDAP and get the title info.
    Function getTitle(userid)
        Dim objConnection
        Dim objCommand
        Dim objRecordSet

        '======== Set up ADODB Connection 
        objConnection = CreateObject("ADODB.Connection")
        objConnection.Provider = "ADsDSOObject"
        objConnection.Open("LDAP Provider;")
        objCommand = CreateObject("ADODB.Command")
        objCommand.ActiveConnection = objConnection

        objCommand.CommandText = "SELECT TITLE FROM 'LDAP://ldapapi/ou=people,o=my domain' WHERE unixid='" & userid & "'"
        objRecordSet = objCommand.Execute

        On Error Resume Next

        While Not objRecordSet.EOF
            For Each i In objRecordSet.fields
                For Each v In i.value

                    Console.WriteLine(v) 'the info I need

        End While


    End Function

End Module

Open in new window

The code could certainly be cleaned up, though.

Author Comment

ID: 39920639
That worked!  Is there a better way i should be doing this?
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39920694
We usually try to avoid late-binding (e.g. CreateObject) in .NET. There is nothing technically wrong with using it, but code is usually less error-prone with early-binding--that is, having the actual types of your objects known at compile time. You might look into some of the classes .NET has available for working with LDAP. Here are a few tutorials:


Author Closing Comment

ID: 39920747
Perfect!  Thanks for the help!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question