Search code examples
c#asp.netoledbaspxgridviewoledbconnection

The Microsoft.ACE.OLEDB.12.0 provider IS registered on the local machine but keep getting this error


I know this question has been asked plenty but I can't find a fix. I am trying to import an excel file and bind it to an aspxgridview. I have installed the AccessDatabaseEngine_X64.exe redistributable on my local machine, but I keep geeting the "The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine". below is a snippet of code.

string connStr = "";
            string ext = Path.GetExtension(fuImportPP.FileName).ToLower();
            string path = Server.MapPath("~/ExcelToGrid/" + fuImportPP.FileName);

            gv = new GridView();

            fuImportPP.SaveAs(path);

            //if (ext.Trim() == ".xls")
            //    //connection string for that file which extantion is .xls  
            //    connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            //else if (ext.Trim() == ".xlsx")
                connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

            string query = "SELECT * FROM [GridViewExport$]";

            OleDbConnection conn = new OleDbConnection(connStr);

            if (conn.State == ConnectionState.Closed)
                conn.Open();

            OleDbCommand cmd = new OleDbCommand(query, conn); 
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            DataSet ds = new DataSet();

            da.Fill(ds);
            gv.DataSource = ds.Tables[0];
            gv.DataBind();
            phGridViewHolder.Controls.Add(gv);
            conn.Close(); 

Solution

  • First check the bitness of your office installation

    Since you're using c#, Try and go to Project > Project Properties > Click on the Build Tab > Where it Platform Target > Change it so that for example if you Office Installation is 32 Bit then change it to 32 Bit and if it is 64 Bit then change it to 64 Bit.

    After that save and try running your project again.