Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:26:41+00:00 2026-05-23T09:26:41+00:00

This following code works correctly the first time I access the file to do

  • 0

This following code works correctly the first time I access the file to do a line count. However, when I attempt to open the same file to read the data, I get an error thrown on ExcelConnection.Open() which states that I do not have permission to access the file or it is already being used. Any thought as to why the file isn’t being released after the first connection to it?

public static DataSet GetExcelWorkSheet(string pathName, string fileName)
        {
            string fileExtention = System.IO.Path.GetExtension(fileName).ToLower();

            OleDbConnection ExcelConnection = new OleDbConnection(fileExtention == ".xls" ?
                @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + @"\" + fileName + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1;ReadOnly=true;\"" :
                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathName + @"\" + fileName + ";Extended Properties=\"Excel 12.0;HDR=No;IMEX=1;ReadOnly=true;\"");
            OleDbCommand ExcelCommand = new OleDbCommand();
            ExcelCommand.Connection = ExcelConnection;
            OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
            ExcelConnection.Open();

            // DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);

            string CheckSheetName = ExcelSheets.Rows[0]["TABLE_NAME"].ToString();
            string SpreadSheetName;
            int useMe = -1;

            if (CheckSheetName.ToUpper() != "SHEET1$")                 // Ok, they have renamed things
            {
                if (CheckSheetName.Substring(0, 5).ToUpper() == "SHEET")
                // if it does START with sheet then look for anything that
                // DOES NOT start with sheet
                {
                    for (int x = 0; x < ExcelSheets.Rows.Count; x++)
                    {
                        if (ExcelSheets.Rows[x]["TABLE_NAME"].ToString().Substring(0, 5).ToUpper() != "SHEET")   // If is does not equal sheet then use it
                        {
                            useMe = x;
                        }
                    }
                }
            }

            SpreadSheetName = string.Format("[{0}]", useMe == -1 ? CheckSheetName : ExcelSheets.Rows[useMe]["TABLE_NAME"].ToString());

            try
            {
                DataSet ExcelDataSet = new DataSet();
                ExcelCommand.CommandText = @"SELECT * FROM " + SpreadSheetName;
                ExcelAdapter.Fill(ExcelDataSet);
                return ExcelDataSet;
            }
            catch (Exception)
            {
                ExcelConnection.Close();
                return new DataSet();
            }
            finally
            {
                // Clean up.
                if (ExcelConnection != null)
                {
                    ExcelConnection.Close();
                    ExcelConnection.Dispose();
                }
                if (ExcelSheets != null)
                {
                    ExcelSheets.Dispose();
                }
            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:26:42+00:00Added an answer on May 23, 2026 at 9:26 am

    You must close the connetion after the file has been read or the operation you have odne is completed….

    ExcelConnection.Close();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm trying the following code. This works most of the time (on
I have the following code: Bear in mind that while this code works on
Never thought I'd have this problem :) The following snippet of code works in
The following code doesn't work! 'this' in in the context when i access it
How can I do this? (The following code does NOT work, but I hope
I have the following code in a shell script. This only seems to work
Work on this small test application to learn threading/locking. I have the following code,
I've got the following code: Regex.Replace(text, words.ToString(), <dfn title=\ + this.FindDefinition($0) + \>$0</dfn>, RegexOptions.IgnoreCase
following up from yesterday... This portion of the code does work. $(document).ready(function(){ $('#listMenu a').click(function
This following code gets this compile error: " invalid types 'int[int]' for array subscript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.