Search code examples
c#asp.netexceloledb

Unspecified error when opening connection with excel file


I am working on asp.net c# application in which i am taking excel file from the user and after uploading it to server , i am printing the contents of this file on my page , upload is fine but when i open the connection Unspecified error occured at connExcel.Open(); in the following code :

  private void ImportToGrid(string File_Path, string Extenstion)
    {        
            string ConnStr = "";
            switch (Extenstion)
            {
                case ".xls": //Excel 97-03
                    ConnStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                    break;
                case ".xlsx": //Excel 07
                    ConnStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                    break;
            }
            ConnStr = String.Format(ConnStr, File_Path, "No");
            OleDbConnection connExcel = new OleDbConnection(ConnStr);
            OleDbCommand cmdExcel = new OleDbCommand();
            OleDbDataAdapter oda = new OleDbDataAdapter();
            DataTable dt = new DataTable();
            cmdExcel.Connection = connExcel;

            connExcel.Open();
            DataTable dtExcelData;
            dtExcelData = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                connExcel.Close();

                connExcel.Open();
                cmdExcel.CommandText = "SELECT * From [Sheet1$]";
                oda.SelectCommand = cmdExcel;
                oda.Fill(dt);
                connExcel.Close();
                GridView_Excel.Caption = Path.GetFileName(File_Path);
                ViewState["GridviewData"] = dt;
                GridView_Excel.DataSource = ViewState["GridviewData"] as DataTable;
                GridView_Excel.DataBind();        
    }

The code is working fine on development server.

Please help me :(


Solution

  • i think it is all related to office and AccessDatabaseEngine_X64.exe For me i had to install office 2010 64 on the hosting server and enable 32 bit on iis - application pool . and install AccessDatabaseEngine_X64.exe

    then restart the server or iis and everything will work fine with you . it has nothing to do with code i guess , i because i have this problem with some servers and some not.