Search code examples
c#asp.net-core-mvcentity-framework-coreasp.net-core-2.2csvhelper

How to get csv row count in CsvHelper


Im working with CsvHelper for my Asp.net Core 2.2 MVC EF Application. I can read the data and insert it to my database after I upload the csv comma delimited file. but when the csv file has many rows inside, it only insert the first row and even if the csv has 10 rows inside. below is my Controller code: I hope you can help me. TIA.

[HttpPost("textCsv")]
        public async Task<IActionResult> textCsv([Bind("CompanyCode,ProductCode,TransactionDate,TransactionTime,OriginatingBranch,CustomerNumber,TransactionOrigin,TypeOfPayment,CheckNumber,TransactionAmount,CustomerName,UserName,UserIP,UserDate")]BdoToDb bdoToDb, List<IFormFile> files)
        {
            UserDetails();
            long size = files.Sum(f => f.Length);

            // full path to file in temp location
            var filepath = Path.GetTempFileName();
            var users = new List<BDOCsv>();
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    using (var stream = formFile.OpenReadStream())
                    {
                        try { users = stream.CsvToList<BDOCsv>(); }
                        catch (Exception ex) { return BadRequest(ex.Message); }
                    }
                }
            }
            int i = 0;
            while (i < users.Count)
            {
                ViewBag.CompanyCode = users[i].CompanyCode;
                ViewBag.ProductCode = users[i].ProductCode;
                ViewBag.TransactionDate = users[i].TransactionDate;
                ViewBag.TransactionTime = users[i].TransactionTime;
                ViewBag.OriginatingBranch = users[i].OriginatingBranch;
                ViewBag.CustomerNumber = users[i].CustomerNumber;
                ViewBag.TransactionOrigin = users[i].TransactionOrigin;
                ViewBag.TypeOfPayment = users[i].TypeOfPayment;
                ViewBag.CheckNumber = users[i].CheckNumber;
                ViewBag.TransactionAmount = users[i].TransactionAmount;
                ViewBag.CustomerName = users[i].CustomerName;

                bdoToDb.CompanyCode = ViewBag.CompanyCode;
                bdoToDb.ProductCode = ViewBag.ProductCode;
                bdoToDb.TransactionDate = ViewBag.TransactionDate;
                bdoToDb.TransactionTime = ViewBag.TransactionTime;
                bdoToDb.OriginatingBranch = ViewBag.OriginatingBranch;
                bdoToDb.CustomerNumber = ViewBag.CustomerNumber;
                bdoToDb.TransactionOrigin = ViewBag.TransactionOrigin;
                bdoToDb.TypeOfPayment = ViewBag.TypeOfPayment;
                bdoToDb.CheckNumber = ViewBag.CheckNumber;
                bdoToDb.TransactionAmount = ViewBag.TransactionAmount;
                bdoToDb.CustomerName = ViewBag.CustomerName;
                bdoToDb.UserName = ViewBag.DisplayName;
                bdoToDb.UserIP = HttpContext.Connection.RemoteIpAddress.ToString();
                bdoToDb.UserDate = DateTime.Now.ToString("MM/dd/yyyy");
                i++;
                _context.Add(bdoToDb);
                await _context.SaveChangesAsync();
            }
            //return View();
            return RedirectToAction(nameof(Index));
        }


Solution

  • The line

    try { users = stream.CsvToList<BDOCsv>(); }
    

    overwrites the users list every time it reads a file. So when you do

    while (i < users.Count)
    

    the users list only contains the last user file that was read in.

    Try changing it to

    try { users.AddRange(stream.CsvToList<BDOCsv>(); }
    

    This will append the new users to the existing list, instead of replacing them.