错误:无法将类型'string'隐式转换为'int? '/创建广告用户阶段时出现此错误, [英] Error: cannot implicitly convert type 'string' to 'int? ' / Get this error when creating ad users stage ,

查看:82
本文介绍了错误:无法将类型'string'隐式转换为'int? '/创建广告用户阶段时出现此错误,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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());

                }
            }
        }
    }
}





我尝试了什么:



如果我将属性分配给文本,例如name,但是当我分配时,我能够创建用户尝试使用它不喜欢字符串的csv文件。



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.

推荐答案

我们无法分辨 - 它需要你的代码运行,你的数据要找出什么正在进行。



所以,这取决于你。

幸运的是,你有一个工具可以帮助你你会发现发生了什么:调试器。一个快速的谷歌Visual Studio调试器应该为您提供所需的信息。



在函数的第一行放置一个断点,并通过调试器。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
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!


这篇关于错误:无法将类型'string'隐式转换为'int? '/创建广告用户阶段时出现此错误,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆