none
Visual Studio 2017 crashes when I paste in a large amount of C# RRS feed

  • Question

  • I have a .csv file with some hierarchical data that I need to import to a database. I wrote some C# to parse the .csv file and produce the C# classes that would do this. However, due to the fact that there are a lot of entries in the .csv file, this produced almost 34,000 lines of C#.

    When I try to paste this in to my project, VS crashes.

    Anything I can do about this? I realise that 34,000 lines is a lot, but that's the amount of data I have. Is there a way to persuade VS to handle a big file? PC memory isn't an issue, I have 40Gb, it seems to be VS that's the problem.

    Thanks

    Monday, June 18, 2018 1:07 PM

All replies

  • Hi Mr Yossu,

    I don't know why you would paste a CSV file into your project.  With that in mind, have you looked for samples of CSV file parsers written in C#?  For example here is a simple one https://www.codeproject.com/Tips/741941/CSV-Parser-Csharp

    Essentially the C# application will open the CSV file and parse it, record by record.

    Sincerely,

    IoTGirl

    Wednesday, June 20, 2018 9:38 PM
  • I didn't paste csv into my app, I pasted the C# that was generated by parsing the csv in.

    Either way, the question remains the same, is there any way to have VS handle large files?

    Thursday, June 21, 2018 12:38 PM
  • Hi Mr Yossu,

    This makes no sense to me "I pasted the C# that was generated by parsing the csv in".  If you see the sample I suggested, it is C# code that parses a csv file. There should be no pasting required.

    Sincerely,

    IoTGirl

    Friday, June 22, 2018 12:48 AM
  • Let me explain again then.

    I have a csv file. I wrote some C# (in LinqPad as it happens) to parse that csv file (using the csv file parser in the Microsoft.VisualBasic namespace) and generate C# like this...

    List<Gribble> gribbles = new List<Gribble> {
      new Gribble {
        Name = "Jim",
        Age = 42;
      },
      new Gribble {
        Name = "Fred",
        Age = 36;
      },
      // etc...
    }

    I then wanted to use this C# in a project, so attempted to paste it in. VS crashed as there were too many lines for it to handle.

    You are confusing the two uses of C# here. I used C# in LinqPad to parse the csv file and generate more C# that represented my data. I then wanted to use that generated C# in a VS project. This is not the first time I've written C# to generate C#, it's a common meta-programming technique.

    However, the method in which I got my C# is actually irrelevant, as my main question is why I can't paste in a large amount of C# into VS, irrespective of how I got that C#.

    Does that make it any clearer?

    Friday, June 22, 2018 3:23 PM
  • Hi Mr Yossu,

    Yes, I am confused as to why you would ever do this.  It looks to me like you are complicating your life by parsing a CSV file twice.  Not sure why you want LinqPad in the way to just recreate the CSV file in a more massive document. 

    My suggestion is to read the CSV file directly into the list and avoid the expensive double processing you are doing here.  Sample available at https://stackoverflow.com/questions/26790477/read-csv-to-list-of-objects

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    
    namespace CsvDemo
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<DailyValues> values = File.ReadAllLines("C:\\Users\\Josh\\Sample.csv")
                                               .Skip(1)
                                               .Select(v => DailyValues.FromCsv(v))
                                               .ToList();
            }
        }
    
        class DailyValues
        {
            DateTime Date;
            decimal Open;
            decimal High;
            decimal Low;
            decimal Close;
            decimal Volume;
            decimal AdjClose;
    
            public static DailyValues FromCsv(string csvLine)
            {
                string[] values = csvLine.Split(',');
                DailyValues dailyValues = new DailyValues();
                dailyValues.Date = Convert.ToDateTime(values[0]);
                dailyValues.Open = Convert.ToDecimal(values[1]);
                dailyValues.High = Convert.ToDecimal(values[2]);
                dailyValues.Low = Convert.ToDecimal(values[3]);
                dailyValues.Close = Convert.ToDecimal(values[4]);
                dailyValues.Volume = Convert.ToDecimal(values[5]);
                dailyValues.AdjClose = Convert.ToDecimal(values[6]);
                return dailyValues;
            }
        }
    }

    Sincerely,

    IoTGirl

    Friday, June 22, 2018 5:29 PM
  • I'm confused as to why you can't understand my explanation! Where did I say I was reading the csv file twice? I said clearly that I was reading it in my LinqPad script and using the data to generate the C# for the objects. Once that is done, I don't read the csv file again.

    The second stage was to take the generated C# and use it in a VS project. That's where VS crashed. As I said before, the fact that the generated C# came from a csv file is irrelevant, it could just have easily been written by hand.

    Anyway, I don't think we're getting anywhere here. I ended up going about this a different way, and avoided the problem. However, I would still like to know why VS can't handle big files. Forget csv, forget LinqPad, just look at that one question. Why is it that on a machine with 40Gb of RAM, VS crashes when trying to load a large .cs file?

    Sunday, June 24, 2018 12:57 PM
  •  I said clearly that I was reading it in my LinqPad script and using the data to generate the C# for the objects.

    This is *exactly* the step that makes no sense to me. You can read the CSV directly rather than bloat it into a massive data definition file first. I assume avoiding this step solved your problem.

    Sunday, June 24, 2018 10:37 PM
  • This is *exactly* the step that makes no sense to me. You can read the CSV directly rather than bloat it into a massive data definition file first. I assume avoiding this step solved your problem.

    No, but as I keep saying, it's irrelevant to my actual question, which is why VS can't handle a large file. I wish I'd never mentioned the origin of the C#, it seems to have thrown you off on a complete tangent.
    Monday, June 25, 2018 12:37 PM
  • Hi Mr Yossu,

    I have opened massive files in VS. It pages in the content being viewed as needed so I am not sure what else is going on with your set up but my advice remains the same. Avoid creating or opening the extra file you created that likely has issues and use the CSV directly.

    Sincerely,

    IoTGirl

    Monday, June 25, 2018 6:43 PM
  • >When I try to paste this in to my project, VS crashes.

    Is it just when you paste the code in that the problem occurs?
    What happens if you use some other editor to create the .cs file, can VS open and compile it then?

    Dave

    Monday, June 25, 2018 7:18 PM
  • >When I try to paste this in to my project, VS crashes.

    Is it just when you paste the code in that the problem occurs?
    What happens if you use some other editor to create the .cs file, can VS open and compile it then?

    Dave

    I tried pasting it in, and I tried adding it to the file outside of VS, then opening it in VS. Either way, VS crashed.

    Thanks

    Monday, June 25, 2018 7:22 PM
  • >I tried pasting it in, and I tried adding it to the file outside of VS, then opening it in VS. Either way, VS crashed.

    OK, so can you post the simplest project that reproduces the crash somewhere that anyone can try it - such as on a OneDrive share?

    Dave

    Monday, June 25, 2018 7:44 PM