9

I have been asked to port a WinForms app that uses the MVP pattern over to a webpage. The app, amongst other things, uploads a CSV file to a DataTable and then does some work.

The CSV file is uploaded to the server OK and then read with the following code

string connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Extensions=asc,csv,tab,txt;Persist Security Info=False;Dbq=C:\Temp\";

//check that file exists and in correct format
if (File.Exists(this.WorkingFileName))
{                    
    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        // Determine number of rows
        string selectCount = "select count(*) from [MyFile.csv]");

        using (OdbcCommand command = new OdbcCommand(selectCount, connection))
        {
            connection.Open();
        }
    }
}

at this point I get the error:

ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

Now the code works fine in WinForms but fails on the web. Is there something I need to change in IIS, my config file or something else to get this code to work? Or is there something more fundamental I need to do?

Update

OK so I worked out what was different between my two code versions: The WinForms version was running as 32-bit, as soon as I changed it to 64-bit it threw the same error. See: 32-bit Text drivers (Microsoft Access , Microsoft Excel and Text files ) from a 64 bit application on windows 7

To fix things I installed the Access 64-bit drivers from http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13255 but I still get the same error.

If I check my ODBC Data Source Administrator I can see "Microsoft Access Text Driver (*.txt, *.csv) | 14.00.47600.1000 | Microsoft Corporation | ACEODBC.dll

So it looks like they're installed OK, so why would it still be failing?

Community
  • 1
  • 1
openshac
  • 4,966
  • 5
  • 46
  • 77

3 Answers3

13

OK, I found the problem. Just to summarise all the parts to my solution.

  1. Uninstall any 32-bit Office apps (required for step 2)
  2. Install the Access 64-bit drivers
  3. Re-install any 32-bit Office apps
  4. Change the connection string in TWO places as can be seen here to:

    @"Driver={Microsoft Access Text Driver (*.txt, *.csv)};Extensions=asc,csv,tab,txt;Persist Security Info=False;Dbq=C:\Temp\"

Note that:

  1. The driver name has been changed to Microsoft Access Text Driver
  2. The delimiter for the file extensions has been changed from a semi-colon to a comma.

I didn't spot the comma change which caused me a lot of pain :-(

openshac
  • 4,966
  • 5
  • 46
  • 77
  • 1
    I'm not saying this was an option for you (you didn't affirm or deny the requirement), but what about kicking back to 32 bit? Assuming IIS 7+, did you try running the app pool in Enable 32-Bit Applications mode? Works on my machine! :) – mlhDev Sep 11 '12 at 01:59
  • Yeah, my requirement was to get it working on a 64 bit web server – openshac Sep 11 '12 at 10:44
  • Bravo! I've been struggling with this issue for weeks until I found your solution,. – Manny Ramirez May 31 '14 at 07:56
  • @openshac the link in point 4 is broken, Can you update that. Thanks – JNL Oct 14 '14 at 20:16
  • 1
    @JNL Cheers, I've fixed that now. – openshac Oct 15 '14 at 08:57
  • While this answer helped me, I was lazy and didn't want to uninstal my 32-bit version of office. The following link helped me. Essentially run the driver pack with "/passive" and then remove a registry value afterwards. http://knowledge.autodesk.com/support/autocad-civil-3d/troubleshooting/caas/sfdcarticles/sfdcarticles/How-to-install-64-bit-Microsoft-Database-Drivers-alongside-32-bit-Microsoft-Office.html – ggenglish Aug 31 '15 at 17:43
2

This is probably due to the webserver not having the Jet Library installed, which I believe provides the Text Driver. It is probably installed locally due to coming with MS Office (again, I believe this is the case)

ChrisBint
  • 12,773
  • 6
  • 40
  • 62
  • I am running the code on my own PC in both cases so anything that needs to be installed is already installed. – openshac Jun 27 '11 at 09:31
0

Is the connection string well formed? It seems that the dbq parameter is wrong.

"Driver={Microsoft Text Driver (*.txt; *.csv)};Extensions=asc,csv,tab,txt;Persist Security Info=False;Dbq=**C:Temp\\**"

Also take a look at this link, provides useful connection string samples

Rodrigo
  • 4,365
  • 3
  • 31
  • 49
  • Thanks @Rodrigo, yes I managed to insert a typo into my connection string in my original post. I have corrected the original question now. – openshac Jun 27 '11 at 11:04