0

Based on the code posted by the OP here (which theoretically should work for me with the well-formatted test file I've got), I'm trying this to assign the contents of a CSV file to a DataGridView:

string conStr = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + selectedFile + ";Extensions=csv,txt";

OdbcConnection conn = new OdbcConnection(conStr);

OdbcDataAdapter da = new OdbcDataAdapter("Select * from [" + Path.GetFileName(selectedFile) + "]", conn);
DataTable dt = new DataTable(selectedFile);
da.Fill(dt);

dataGridViewFileContents.DataSource = dt;

da.Dispose();
conn.Close();
conn.Dispose();

When running through my test scenario, the value of selectedFile is C:\PersianUtil\persianUtilOutput.txt and the value of conStr is Driver={Microsoft Text Driver (.txt; .csv)};Dbq=C:\PersianUtil\persianUtilOutput.txt;Extensions=csv,txt

But on the "da.Fill(dt);" line I'm getting:

System.Data.Odbc.OdbcException was unhandled
  HResult=-2146232009
  Message=ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' 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.
ERROR [01000] [Microsoft][ODBC Text Driver]General Warning Unable to open registry key 'Temporary 
(volatile) Jet DSN for process 0x26d4 Thread 0x11d8 DBC 0x8649fd4 Text'.
ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed
ERROR [01000] [Microsoft][ODBC Text Driver]General Warning Unable to open registry key 'Temporary 
(volatile) Jet DSN for process 0x26d4 Thread 0x11d8 DBC 0x8649fd4 Text'.
ERROR [HY024] [Microsoft][ODBC Text Driver] '(unknown)' is not a valid path.  . . .

What am I doing wrong or failing to do here?

If there's a better way to do this / alternate methodology, I'm open to that, too.

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

1

I know it's old question, but we started getting this error on files that worked before. After restarting Excel, we are again able to open it until the next time the error shows up.

It seems like Excel is acting up and not closing some of the exclusive locks as explained nicely here.

Were you able to troubleshoot it?

Community
  • 1
  • 1
Stoinov
  • 774
  • 12
  • 25