Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Collections;
using Microsoft.VisualBasic.FileIO;

namespace ADBulkImport
{
    public partial class frmBulkImport : Form
    {
        public frmBulkImport()
        {
           InitializeComponent();
        }

        private void btnBulkImport_Click(object sender, EventArgs e)
        {
            //Path to main OU
            string strPath = "LDAP://OU=Test Students,OU=Users,OU=Client Services,DC=esc,DC=local";
            //reading csv 
            string csvFilePath = @"T:\jake\Account creator messing\test files\StudentAccount.csv";
            TextFieldParser csvReader = new TextFieldParser(csvFilePath);
            csvReader.SetDelimiters(new string[] { "," });
            csvReader.HasFieldsEnclosedInQuotes = true;

            // reading column fields 
            string[] colFields = csvReader.ReadFields();
            string index_GivenName = colFields.ToList().IndexOf("GivenName").ToString();
            string index_Surname = colFields.ToList().IndexOf("Surname").ToString();
            string index_samaccountName = colFields.ToList().IndexOf("samAccountName").ToString();
            int index_ID = colFields.ToList().IndexOf("ID");
            string index_Course = colFields.ToList().IndexOf("Course").ToString();
            while (!csvReader.EndOfData)
                


            {
                
                try
                {
                    
                    string st = File.ReadAllText(csvFilePath);              

                    Console.WriteLine(st);                                  

                    DirectoryEntry objDEOU = new DirectoryEntry(strPath);   

                    ArrayList objUserList = new ArrayList();                

                    DirectoryEntry objDEUser = new DirectoryEntry(strPath);

                    string email = index_ID + "@student.esc.ac.uk";
                    var strUserName = index_GivenName + index_Surname + index_ID;
                    DirectoryEntry objUser = objDEUser.Children.Add("CN=" + strUserName, "user");

                    //DirectoryEntry objUser = ouEntry.Children.Add("CN=" + csvData[index_samaccountName], "user");

                    var csvData = csvReader.ReadFields();
                    var fieldData = csvReader.ReadFields();               
<code></code>
                    DirectoryEntry ouEntry = new DirectoryEntry(strPath);

                    //User Name


                    // DirectoryEntry objUser = objDEUser.Children.Add("CN=" + strUserName, "user");

                    //objUser.Properties["samaccountname"].Value = csvData[index_GivenName + index_Surname + index_ID];
                    MessageBox.Show(csvData[index_samaccountName]);

                    // User name (domain based)   
                    objUser.Properties["userprincipalname"].Value = csvData[index_samaccountName].ToString();


                    // User name (older systems)  
                    objUser.Properties["samaccountname"].Value = csvData[index_GivenName + index_Surname + index_ID];
                 
                    // Surname  
                    objUser.Properties["sn"].Value = csvData[index_Surname].ToString;
                   
                    // Forename  
                    objUser.Properties["givenname"].Value = csvData[index_GivenName];
                    
                    // Display name  
                    objUser.Properties["displayname"].Value = csvData[index_GivenName + index_Surname + index_ID];
                    
                    // Description  
                    objUser.Properties["description"].Value = csvData[index_Course];

                    // E-mail  
                    //objUser.Properties["mail"].Value = csvData(index_ID + "@student.esc.ac.uk");

                    
                    objUser.CommitChanges();
                   
                    objDEUser.CommitChanges();
                    

                    //Password
                    objUser.Invoke("SetPassword", new object[] { "second@123" });
                 
                    objUser.CommitChanges();
                   

                    //Adding created user to list
                    objUserList.Add(strUserName);
                    MessageBox.Show("10");


               

                  


                    MessageBox.Show("Process Completed...");
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    MessageBox.Show("Failed ...");
                    Console.WriteLine(ex.ToString());
                    Console.WriteLine(System.Runtime.InteropServices.Marshal.GetExceptionCode());

                }
            }
        }
    }
}


What I have tried:

Am able to create a user if i assign the properties to texts eg "name" but when i try and use a csv file it doesn't like the string.
Posted
Updated 8-Apr-19 4:37am

1 solution

We can't tell - it needs your code running, and your data to find out what is going on.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. A quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900