1

Guys am trying to read a csv file from the view, get its data , format it and write back into an empty csv file. presently have been able to achieve the first approach, where the challenge is right now is writing back into an empty csv file created , but it happens that the csv file is always blank after writing in the file. can someone help me out if am missing anything. Note " am just reading and writing the field , have got nothing to do with the hearder because the csv file has no header" Am using csvhelper library.

        if (file.ContentLength > 0)
        {

            string origin = "FORMATERCSV";

            string destination = "FORMATERCSVDESTINATION";
            string curretnDate = Convert.ToString(DateTime.Now.ToShortDateString().Replace(@"/", "_"));
            var fileName = Path.GetFileName(file.FileName);

            var pathfound = Server.MapPath( @"/" + "Content" + "/" + origin + "/" +  curretnDate + "/");
            var pathfoundDestination  = Server.MapPath(@"/" + "Content" + "/" + destination + "/" + curretnDate + "/");
            if (!Directory.Exists(pathfound)) Directory.CreateDirectory(pathfound);

            if (!Directory.Exists(pathfoundDestination)) Directory.CreateDirectory(pathfoundDestination);



            string PathToStore = string.Format(@"{0}\{1}", pathfound, fileName);

            string PathToStoreDestination = string.Format(@"{0}\{1}", pathfoundDestination, fileName);
            var path = Path.Combine(pathfound,fileName);
            file.SaveAs(PathToStore);

            file.SaveAs(PathToStoreDestination);
            System.IO.File.WriteAllText(PathToStoreDestination,string.Empty);



            StreamReader sr = new StreamReader(PathToStore);


            CsvReader csvread = new CsvReader(sr);

            csvread.Read();

            var shedule = new Shedule()
            {
                RSA_PIN = csvread.GetField<string>(0),
                EMPLOYEE_NAME = csvread.GetField<string>(1),
                EMPLOYER_CONTRIBUTION = csvread.GetField<double>(2),
                EMPLOYER_VC = csvread.GetField<double>(3),
                EMPLOYEE_CONTRIBUTION = csvread.GetField<double>(4),
                EMPLOYEE_VC = csvread.GetField<double>(5),
                TOTAL_CONTRIBUTION = csvread.GetField<double>(6),
                FROM_MONTH = csvread.GetField<string>(7),
                FROM_YEAR = csvread.GetField<string>(8),
                TO_MONTH = csvread.GetField<string>(9),
                TO_YEAR = csvread.GetField<string>(10),
                EMPLOYER_CODE = csvread.GetField<string>(11),
                EMPLOYER_NAME = csvread.GetField<string>(12),
                PTID = csvread.GetField<string>(13),
                RECEIVED_DATE = csvread.GetField<string>(14),



            };





            StreamWriter sw = new StreamWriter(PathToStoreDestination);
            CsvWriter scvwrite = new CsvWriter(sw);
            scvwrite.WriteField(shedule.RSA_PIN);
            scvwrite.WriteField(shedule.EMPLOYER_CONTRIBUTION);
            scvwrite.WriteField(shedule.EMPLOYER_VC);
            scvwrite.WriteField(shedule.EMPLOYEE_CONTRIBUTION);
            scvwrite.WriteField(shedule.EMPLOYEE_VC);
            scvwrite.WriteField(shedule.TOTAL_CONTRIBUTION);
            scvwrite.WriteField(shedule.FROM_MONTH);
            scvwrite.WriteField(shedule.FROM_YEAR);
            scvwrite.WriteField(shedule.TO_MONTH);
            scvwrite.WriteField(shedule.TO_YEAR);
            scvwrite.WriteField(shedule.EMPLOYER_CODE);
            scvwrite.WriteField(shedule.EMPLOYEE_NAME);
            scvwrite.WriteField(shedule.PTID);
            scvwrite.WriteField(shedule.RECEIVED_DATE);
            scvwrite.NextRecord();
            scvwrite.Flush();

            // Gets field by position returning int
            //  var field = csv.GetField<int>(0);
        }

        return RedirectToAction("Index");
    }
3
  • 1
    Did you debug the code? Are you getting any values in properties of shedule? Commented Apr 20, 2018 at 14:10
  • @chetan i am getting values in the property of the shedule which i got from the uploaded csv from the view Commented Apr 20, 2018 at 14:17
  • check the 4) of the answer, it should do it. regards Commented Apr 20, 2018 at 14:18

2 Answers 2

1

Several things could actually occure.

1) are you sure the file from the view is not empty?

2) If you use a break point when you instantiate your class Schedule. Do you get the data from the CSV.

3) Do you really need to do this 2 step, wouldn't it be better to directly write the content of the original file to the new file?

4) Last but not least don't forget to close your streamwriter or do like so :

using(var sw = new StreamWriter(PathToStoreDestination)){

        CsvWriter scvwrite = new CsvWriter(sw);
        scvwrite.WriteField(shedule.RSA_PIN);
        scvwrite.WriteField(shedule.EMPLOYER_CONTRIBUTION);
        scvwrite.WriteField(shedule.EMPLOYER_VC);
        scvwrite.WriteField(shedule.EMPLOYEE_CONTRIBUTION);
        scvwrite.WriteField(shedule.EMPLOYEE_VC);
        scvwrite.WriteField(shedule.TOTAL_CONTRIBUTION);
        scvwrite.WriteField(shedule.FROM_MONTH);
        scvwrite.WriteField(shedule.FROM_YEAR);
        scvwrite.WriteField(shedule.TO_MONTH);
        scvwrite.WriteField(shedule.TO_YEAR);
        scvwrite.WriteField(shedule.EMPLOYER_CODE);
        scvwrite.WriteField(shedule.EMPLOYEE_NAME);
        scvwrite.WriteField(shedule.PTID);
        scvwrite.WriteField(shedule.RECEIVED_DATE);
        scvwrite.Flush();
}

Doing so you don't even need to specify to flush.

Sign up to request clarification or add additional context in comments.

2 Comments

@nerever still the same result
even if you add scvwrite.Flush(); ? @user3169144 it might be because your file is actually still open try to delete it restart your ide maybe.
1
            using (var sw = new StreamWriter(PathToStoreDestination))
            {
                sw.AutoFlush = true;

                CsvWriter scvwrite = new CsvWriter(sw);
                scvwrite.WriteField(shedule.RSA_PIN);
                scvwrite.WriteField(shedule.EMPLOYEE_NAME);
                scvwrite.WriteField(shedule.EMPLOYER_CONTRIBUTION);
                scvwrite.WriteField(shedule.EMPLOYER_VC);
                scvwrite.WriteField(shedule.EMPLOYEE_CONTRIBUTION);
                scvwrite.WriteField(shedule.EMPLOYEE_VC);
                scvwrite.WriteField(shedule.TOTAL_CONTRIBUTION);
                scvwrite.WriteField(shedule.FROM_MONTH);
                scvwrite.WriteField(shedule.FROM_YEAR);
                scvwrite.WriteField(shedule.TO_MONTH);
                scvwrite.WriteField(shedule.TO_YEAR);
                scvwrite.WriteField(shedule.EMPLOYER_CODE);
                scvwrite.WriteField(shedule.EMPLOYER_NAME);
                scvwrite.WriteField(shedule.PTID);
                scvwrite.WriteField(shedule.RECEIVED_DATE);

                scvwrite.NextRecord();
                //scvwrite.Flush();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.