Click here to Skip to main content
15,071,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Before I specify my problem, let me tell you all that I've already explored enough on this issue over internet but none of any article help me to solve my issue. The problem is, When I try to upload an excel file and start processing it it throws an error "The Microsoft Access database engine cannot open or write to the file . It is already opened exclusively by another user, or you need permission to view and write its data". My code is working properly on my local machine the main issue comes when i upload code to the production server. Below is my code:



Note: I'm using MySql as database.

I've also set the permissions to the folder on my server where the file get stored after uploading still the same issue.


What I have tried:

if (file_Upload_DataFile.HasFile)
           {
               string fileName =
   Path.GetFileName(file_Upload_DataFile.PostedFile.FileName);
                   string fileExtension = Path.GetExtension(file_Upload_DataFile.PostedFile.FileName);

                   if (fileExtension != ".xls" && fileExtension != ".xlsx")
                   {
                       ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "alert('invalid file format!! you must upload a file having an extention of either (.xls) or (.xlsx)');", true);
                       extFlag = false;
                   }

                   string fileLocation = Server.MapPath("../ExcelReport/" + fileName);
                   file_Upload_DataFile.SaveAs(fileLocation);
                   if (fileExtension == ".xls")
                   {
                       connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                       extFlag = true;
                   }
                   else if (fileExtension == ".xlsx")
                   {
                       connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                       extFlag = true;
                   }

               }
               if (extFlag != false)
               {

                   OleDbConnection con = new OleDbConnection(connectionString);
                   OleDbCommand cmd = new OleDbCommand();
                   cmd.CommandType = System.Data.CommandType.Text;
                   cmd.Connection = con;
                   OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
                   //DataSet ds = new DataSet();
                   DataTable dtExcelRecords = new DataTable();
                   con.Open();
                   DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                   string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
                   cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
                   dAdapter.SelectCommand = cmd;
                   dAdapter.Fill(dtExcelRecords);
                   con.Close();
   }
Posted
Comments
Maciej Los 21-Jun-18 1:55am
   
What kind of permissions you did set?
Rohit Singh 21-Jun-18 2:10am
   
read/write

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900