Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Upload(HttpPostedFileBase upload)
       {
           if (ModelState.IsValid)
           {

               if (upload != null && upload.ContentLength > 0)
               {

                   if (upload.FileName.EndsWith(".csv"))
                   {
                       Stream stream = upload.InputStream;
                       DataTable csvTable = new DataTable();
                       using (CsvReader csvReader =
                           new CsvReader(new StreamReader(stream), true))
                       {
                           csvTable.Load(csvReader);
                       }
                       return View(csvTable);
                   }
                   else
                   {
                       ModelState.AddModelError("File", "This file format is not supported");
                       return View();
                   }
               }
               else
               {
                   ModelState.AddModelError("File", "Please Upload Your file");
               }
           }
           return View();
       }



Error:

C#
Error	1	The type or namespace name 'CsvReader' could not be found (are you missing a using directive or an assembly reference?)


so kindly rectify above mentioned code (MVC 4)

What I have tried:

C#
[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Upload(HttpPostedFileBase upload)
       {
           if (ModelState.IsValid)
           {

               if (upload != null && upload.ContentLength > 0)
               {

                   if (upload.FileName.EndsWith(".csv"))
                   {
                       Stream stream = upload.InputStream;
                       DataTable csvTable = new DataTable();
                       using (CsvReader csvReader =
                           new CsvReader(new StreamReader(stream), true))
                       {
                           csvTable.Load(csvReader);
                       }
                       return View(csvTable);
                   }
                   else
                   {
                       ModelState.AddModelError("File", "This file format is not supported");
                       return View();
                   }
               }
               else
               {
                   ModelState.AddModelError("File", "Please Upload Your file");
               }
           }
           return View();
       }



Error:

C#
Error	1	The type or namespace name 'CsvReader' could not be found (are you missing a using directive or an assembly reference?)


so kindly rectify above mentioned code (MVC 4)
Posted
Updated 25-Feb-16 1:44am

1 solution

The class you're trying to use, CsvReader, is not part of the .NET Framework. It has to be in an external .DLL somewhere. You can probably get that from wherever you got the code you copied.

You then have to add a Reference to that .DLL and then import the namespace exposed by that .DLL that contains the CsvReader class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900