vb.net problem creating multiple users using adsi local computer

NCSA SCADA
NCSA SCADA used Ask the Experts™
on
Hi Experts,  I am trying to create an app that will let you create users on the local computer.  The app will read a list of users from a central db.  That way if a user is no longer needed I just remove it from the db and the next time the app is run that user will not be added.  If I run the code with a hardcoded user name it works great.  If I read from the db I get an invalid username error.  I have tried to remove exta space from the string still does not work.  I am attaching code from the working hardcoded and the db read.  Thanks in anvance for the help
****** Code with hard coded user of "TestUserMe"*******
Try
                Dim AD As New DirectoryEntry("WinNT://" & Environment.MachineName & ",computer")
 Dim NewUser As DirectoryEntry
Try
                    NewUser = AD.Children.Add("TestUserMe", "user")
                    NewUser.Invoke("SetPassword", New Object() {"Password2"})
                    NewUser.Invoke("Put", New Object() {"Description", "This is a test user"})
                    NewUser.Properties("PasswordExpired").Add(1)

                    NewUser.CommitChanges()
                    Dim grp As DirectoryEntry
                    Dim grp2 As DirectoryEntry

                    grp = AD.Children.Find("Administrators", "group")
                    If grp IsNot Nothing Then
                        grp.Invoke("Add", New Object() {NewUser.Path.ToString()})
                    End If

                    grp2 = AD.Children.Find("Users", "group")
                    If grp2 IsNot Nothing Then
                        grp2.Invoke("Add", New Object() {NewUser.Path.ToString()})
                    End If
                    MsgBox("Account Created Successfully")
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try



            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            'End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
****** Code with users pulled from sql"*******
Dim myConnection As SqlConnection
        Dim myCommand As SqlCommand
        Dim dr As SqlDataReader
        'declaring objects for individual conns
        Dim con As New OleDb.OleDbConnection
        'Dim dbProvider As String
        Dim da As New OleDb.OleDbDataAdapter
        Dim ds As New DataSet
        'Dim sql As String
        Dim cb As New OleDb.OleDbCommandBuilder(da)


        myConnection = New SqlConnection("server=10.20.14.11;uid=user;pwd=password;database=SCADA_DATA")

        Try
            myConnection.Open()
            'opening the connection
            myCommand = New SqlCommand("Select * from UserTable", myConnection)
            'executing the command and assigning it to connection 
            dr = myCommand.ExecuteReader()
            While dr.Read()
                'reading from the datareader
                Dim UserName As String = dr(1).ToString()
                TrimAll(UserName)
                'Start loop of new sql command

                Try
                    Dim AD As New DirectoryEntry("WinNT://" & Environment.MachineName & ",computer")


                    Dim NewUser As DirectoryEntry
                   

                    Try
                        NewUser = AD.Children.Add(UserName, "user")
                        NewUser.Invoke("SetPassword", New Object() {"Password2"})
                        NewUser.Invoke("Put", New Object() {"Description", "This is a test user"})
                        NewUser.Properties("PasswordExpired").Add(1)

                        NewUser.CommitChanges()
                        Dim grp As DirectoryEntry
                        Dim grp2 As DirectoryEntry

                        grp = AD.Children.Find("Administrators", "group")
                        If grp IsNot Nothing Then
                            grp.Invoke("Add", New Object() {NewUser.Path.ToString()})
                        End If

                        grp2 = AD.Children.Find("Users", "group")
                        If grp2 IsNot Nothing Then
                            grp2.Invoke("Add", New Object() {NewUser.Path.ToString()})
                        End If
                        MsgBox("Account Created Successfully")
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try



                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try

            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
any thoughts experts

Author

Commented:
I added the following and it resolved the issue, but I would still like to know why....
Any thoughts - If I just point UserName directly at TrimAll it does not work

 Dim UserName As String = dr(1).ToString()
 Dim cleanuser As String = TrimAll(UserName)
Dim dr As SqlDataReader

so dr is an SqlDataReader if you try to trim dr(1) you are trying to trim the  object. That is why you call the toString() method on dr(1). you now set the string UserName to dr(1).ToString . you can trim a string, but you cannot trim an object.

just like if you an integer called myInt. myInt is an integer and you can do math or anything to it. But if you want to do something to it that you can only do to a string then you have to turn myInt into a string.
Dim myString As String = myInt.ToString()

Just like if you want to turn an Enum value into a string you have to call its .ToString() method myValue.someProperty.toString()

Author

Commented:
Thanks for the info.  That helps clear it up

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial