3

I am running an ASP.NET C# application on .NET 4.0 framework using VS2010 on a Win 7 machine. Within my code I want to link Excel file with "DataTable" object. ie I want to access the data within Excel file and store it in DataTable object. So I used the following code snippet:

_

_connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"" + excelFile + "\";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'";
        }

DataTable table = new DataTable();
            OleDbCommand command = new OleDbCommand();
            command.Connection = new OleDbConnection(_connectionString);
            command.CommandType = CommandType.Text;
            command.CommandText = "select * from [NameOFExcelSheet$A1:D20]";  // Want to read the Excel sheet. The name of Excel sheet is "NameOfExcelSheet". Wan to read the celles in the range A1 and D20.

            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(table);  // EXCEPTION OCCURS IN THIS LINE. 

I installed the exe available at the link http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

But still I am gettin the same exception msg while running my code. The exception that I am getting is "The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine"

PLz help me with this.

THANKS IN ADVANCE.

codeLover
  • 3,720
  • 10
  • 65
  • 121
  • 1
    ~39/56 unaccepted answers. You should spare a moment to go through and accept some more answers. – spender May 23 '11 at 11:55
  • I would suggest, when asking about some exception provide details about it: type, message. It will helps - nobody have crystal ball. – VikciaR May 23 '11 at 12:13
  • Hi, the question text has the Exception in it. I have again edited my question and have included it in the body. – codeLover May 23 '11 at 13:18

2 Answers2

3

You're probably on 64bit Windows and installed a 32bit driver. Either switch to 32bit compilation or source a 64bit driver.

spender
  • 117,338
  • 33
  • 229
  • 351
  • Can you plz let me know 1) How to switch to 32bit compilation. OR 2) Provide a 64bit driver. Pardon if I sound silly. I am a fresher in this. Kindly let me know. – codeLover May 23 '11 at 11:57
  • You need to add a new configuration in the "Configuration Manager" under the "build" menu in VS. This MSDN article explains how to go to x64. The steps are the same for x86. http://msdn.microsoft.com/en-us/library/ms185328.aspx – spender May 23 '11 at 12:10
  • You're probably on the right track. However, there is no 64-bit redistributable for that provider and he's using ASP.NET. He has to enable a 32-bit application pool. – Hans Passant May 24 '11 at 00:04
  • Ah. Missed the ASP.NET part of it. Well spotted @Hans – spender May 24 '11 at 00:07
  • I changed it to 32 bit compilation mmode and it worked fine for me. Thanks. :) – codeLover May 25 '11 at 03:17
1

You should try this (ensuring that you are in a x86 (32bits) machine ):

This download will install a set of components that can be used to facilitate transfer of data between 2007 Microsoft Office System files and non-Microsoft Office applications.

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

Otto
  • 4,020
  • 6
  • 35
  • 46
  • Well but my app runs in 64 bit Win 7 machine. How shall I go about it. – codeLover May 23 '11 at 13:20
  • Then you should switch to 32 bit compilation like @spender said Im not sure where its, try: "Properties of project>Compile>Advanced Compile > In the dropdown choose x86 (32 bits)" – Otto May 23 '11 at 14:44