0

I have a code which can download an excel file and it will select all of its data in side. Here is my code:

string conn = string.Empty;
            if (Path.GetExtension(path).ToLower().Equals(".xls"))
            {
                conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
                                + path +
                                ";Extended Properties=\"Excel 8.0;HDR=YES;\"";
            }
            else
            {
                conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
                              + path +
                              ";Extended Properties=\"Excel 12.0;HDR=YES;\"";
            }

            string strselect = "Select * from [Sheet1$]";

            using (OleDbConnection excelCon = new OleDbConnection(conn))
            {
                try
                {
                    excelCon.Open();
                    using (OleDbDataAdapter exDA = new OleDbDataAdapter(strselect, excelCon))
                    {
                        exDA.Fill(_data);
                    }
                }
                catch (OleDbException oledb)
                {
                    throw new Exception(oledb.Message.ToString());
                }
                finally
                {
                    excelCon.Close();
                }
            }

when I debugged it, in excelCon.Open(); I get transferred in my Catch and get an OleDbException: "The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data."

Can you point out what's wrong? I don't have an opened file and I checked its settings but it can be opened by anyone. Any one of you experience this kind of error?

Ms. B
  • 1,073
  • 6
  • 15
  • 26

0 Answers0