Sairam Soham

Sairam Soham

  • NA
  • 47
  • 28.8k

Error while loading the data from csv into dataset

Jan 16 2016 11:21 AM

I wrote the following code:

fileName variable has a value c:\temp\meter.csv

In the debug mode, I am able to view that a correct filename value got assgined to the variable fileName.

But it is throwing an execpetion:

 +  $exception {"'C:\\temp\\meter.csv' is not a valid path.  Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides."} System.Exception {System.Data.OleDb.OleDbException}



 private static DataSet GetDSFromCsv()
        {

            string fileName = Global.CsvFile;
            DataSet ds = new DataSet();

            if (File.Exists(fileName))
            {
                ds.DataSetName = "Reads";
                using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source= "
                    + fileName + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\""))
                {
                    try
                    {
                        using (OleDbCommand command = new OleDbCommand("select * from [" + fileName + "] ", conn))
                        {
                            using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
                            {
                                try
                                {
                                    adapter.Fill(ds);
                                }
                                catch (Exception ex1)
                                {
                                    Console.WriteLine(ex1.ToString());
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }
            return ds;
    }

Question:

Am I missing something?

Thanks


Answers (2)