Prash's Blog

OleDbException is not a valid path Make sure the path name is spelled correctly November 3, 2011

Filed under: ADO — prazjain @ 5:39 pm
Tags: , , , , ,

It has been ages since I did ADO programming. And naturally I hit this common error when loading up my CSV file in memory.

Problem:

      System.Data.OleDb.OleDbException was unhandled

      Message=’C:\CSVTasks\TXT2_RMMTRADE_20101124EOD_20101125_RERUN.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.

      Source=Microsoft JET Database Engine ErrorCode=-2147467259

Reason:

The reason this happens is if you give the full file path in the connection string or if you give just the relative file path when creating Data Adapter using select command text.

example:

            // if you have given full file path below, then it will cause this issue
            string connStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + filePathFull + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"";
            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();

Or if you give just the file name (relative url) when loading file in adapter you will get this exception:

            // if you have given just the file name (not full path) below then it will cause this issue
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + fileName, conn);
            DataSet ds = new DataSet("QueryCSV");
            adapter.Fill(ds);

Solution:

To avoid this issue

  1. You need to give the full path to the directory when your CSV or Text file resides, in the connection string.
  2. You need to give the full file path in the select command text when creating data adapter.

Code sample

    public class CSVReader
    {
        public static DataView GetData(string fileFullPath,string directoryFullPath)
        {
            // give full path to DIR here
            string connStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + directoryFullPath + ";Extended Properties=\"Text;HDR=YES;FMT=Delimited\"";
            OleDbConnection conn = new OleDbConnection(connStr);
            conn.Open();
            // give full path to the file here
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + fileFullPath, conn);
            DataSet ds = new DataSet("QueryCSV");
            adapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            conn.Dispose();
            return dt.AsDataView();
        }
    }
Advertisements
 

One Response to “OleDbException is not a valid path Make sure the path name is spelled correctly”

  1. wstone Says:

    how to get full directory path?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s