web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

migration using a console App

(0) ShareShare
ReportReport
Posted on by 410

I am trying to create a simple console App that reads from a CSV file and creates or updates the fields from the CSV to the traget fields in my D365.  I am using the class TextFieldParser, but getting stuck in the Loop. 

 string[] fields = parser.ReadFields();
                   

                    foreach (var column in fields)
                    {
                        Console.WriteLine($"{column}");
                    }

After looping through, it shows me all the field headers in my CSV, but when i try to get the index of each field using  column[0], rather than return the field, it only returns the first character of the field. I have read through the documentation of the TextfieldParser class, and the parser.ReadField return the fields in arrays. So why is it only retrieving the characters of the fields, when i am trying to get  specific field position?  Someone with experience please help out. 

Cheers

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi

    You will have to do fields[0] to get the field not column[0]

  • Victor Onyebuchi Profile Picture
    410 on at

    hi Kokulan, after using the index for the fields retrieved and enabling Parser.Readline(), rather than get me all the rows in the fields, its repeating the very row for the number of fields present in the CSV file. I want to loop through all the rows of the first fields and get their value.

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Take a look at the following. Seems like an old post and developed in .NET 2.0, but you should be able to use this.

    www.codeproject.com/.../A-Portable-and-Efficient-Generic-Parser-for-Flat-F

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi Boss

    Using your code, if you would like to loop through first column of every row, then you do not need that for loop. Please see the example below

    using (TextFieldParser parser = new TextFieldParser("C:\\test.csv"))
    {
    parser.Delimiters = new string[] { "," };
    while (true)
    {
    string[] parts = parser.ReadFields();
    if (parts == null)
    {
    break;
    }
    else
    {
    Console.WriteLine("Column A values of the row : " + parts[0]);  // This will print value of first column in every row. This is where you want to add your condition.
    }
    }
    }

  • Victor Onyebuchi Profile Picture
    410 on at

    Hi @kokula, the result of the code you sent is just the first row of the first column. I have a table of 12 columns and 100 rows. I want to loop through and display all the rows of the first column. Thanks

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    Hi Boss

    You have to use a couter variable for row count and a inner for loop to loop through columns. In my previous example i only showed how to get to field 0.

    using (TextFieldParser parser = new TextFieldParser("C:\\test.csv"))
    {
    int rowcounter = 0;

    parser.Delimiters = new string[] { "," };
    while (true)
    {
    string[] parts = parser.ReadFields();
    if (parts == null)
    {
    break;
    }
    else
    {
    string columnValues = string.Empty;

    for (int i = 0; i < parts.Length; i++)
    {
    columnValues += ($"Column {i} value is : {parts[i]} ");
    }

    Console.WriteLine($"Row {rowcounter} column values : {columnValues}");
    rowcounter++;
    }

    }
    }

    Please see the output I am getting from the CSV

    ScreenClip-_5B00_707_5D00_.png

  • Victor Onyebuchi Profile Picture
    410 on at

    Thanks kokulan!!

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans