using (var csvReader = new CsvReader(reader)) is raising the error: CsvReader could not be found.

Waithley Williams 1 Reputation point
2021-10-27T02:23:31.543+00:00

I have just started learning to use Visual Studio 2019 Community Edition and C#. I have run into an error that I don't know how to solve. The error is in this line:

using (var csvReader = new CsvReader(reader))

CS0246 -- CvsReader-- "The type of namespace name CvsReader could not be found. The code is as follows:

using System;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using LumenWorks.Framework.IO.Csv;
using System.Globalization;



namespace SMR_SCQ

{
    public partial class Form3
    {
        public Form3(object records)
        {
            InitializeComponent();

            using (var reader = new StreamReader("C: /Users/sales/Desktop/SMR_SCQ_DB/SMR_SCQ/SMR_Databases"))

            using (var csvReader = new CsvReader(reader))
            {
                records = csvReader.GetRecords<DataGridViewElement>().ToList();
            }
        }

Any help will be appreciated.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-10-27T08:20:04.827+00:00

    @Waithley Williams , based on your code, I think you want to convert csv file to list.

    You could try the following steps to solve the problem.

    First, Please remove the reference related to LumenWorks.Framework.IO.Csv.

    Second, Please install nuget-package CsvHelper.

    Third, you could try the following code to get what you want.

    using CsvHelper;  
    using System.Globalization;  
    using System.IO;  
    using System.Linq;  
    using System.Windows.Forms;  
      
    namespace Problem_for_csvreader  
    {  
        public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
      
                using (TextReader reader = new StreamReader("D:\\2.csv"))  
      
                using (var csvReader = new CsvReader(reader,CultureInfo.CurrentCulture,false))  
                {  
                    var records = csvReader.GetRecords<Student>().ToList();  
                
                }  
            }  
        }  
      
      
        public class Student  
        {  
            public string Name { get; set; }  
      
            public int Age { get; set; }  
      
            public int ID { get; set; }  
        }  
    }  
    

    csv and result in winform:

    144099-image.png

    Hope this could help you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments