Question

I am trying to save to a CSV file using the elency solutions csv helper using this code

        private void evSave(object sender, EventArgs e)
    {
        int key = 696969;
        int enc = money ^ key;
        string hexvalue = enc.ToString("X");
        string path = sName + ".csv";

        CsvFile file = new CsvFile();
        CsvRecord rec = new CsvRecord();
        //CsvWriter write = new CsvWriter();
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        rec.Fields.Add(sName);
        rec.Fields.Add(hexvalue);

        file.Records.Add(rec);
        using (CsvWriter write = new CsvWriter())
        {
            write.AppendCsv(file, path);
        }
        file = null;
        rec = null;
        //write = null;
    }

The code in other occasions is saving but strange enough in this one is not working. Practically what i am doing is save a name and an encrypted score to a csv file but it always stops on: write.AppendCsv(file, path) giving me the error Object reference not set to an instance of an object

As you can see i have tried to declare the csv writer in the beginning and i tried also load the class just when i need it but i still get that annoying error.

Let me remind you i am using same type of saving in another part of my application and its working fine can you please help me out with this annoying problem.

Regards.

Was it helpful?

Solution

I have solved it!

The problem is that the sName which is coming from a text box was remaining empty so the path was not saving correctly!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top