Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am getting the below error. Please help me to solve this.

OleDbException was unhandled by user code
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


What I did so far,
1) I have added an file upload control. Code in the button click,
C#
protected void btnUpload_Click(object sender, EventArgs e)
 {
         string strFileExtension = Path.GetExtension(fuList.PostedFile.FileName).ToString().ToUpper();
         string connstr = string.Empty;
         string strFileName = (fuList.PostedFile.FileName).Replace("'", "");
         if (strFileExtension == ".XLSX")
             connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + (fuList.PostedFile.FileName).Replace("'", "") + "';Extended Properties=Excel 12.0";

         else if (strFileExtension == ".XLS")
             connstr = "Provider=Microsoft.Jet.Oledb.4.0;Data Source='" + (fuList.PostedFile.FileName).Replace("'", "") + "';Extended Properties=Excel 8.0";
         OleDbConnection conn = new OleDbConnection(connstr);
         conn.Open();
}

I got the above mentioned error at conn.Open()

2) I have tried the following method to solve this,
(I am using 64 bit windows 7 OS and MS Office 32 bit)
Created directory "C:\Windows\SysWOW64\config\systemprofile\Desktop "
And added NETWORK SERVICE and gave full permission
(Desktop Properties >> Security >> Added NETWORK SERVICE >> And gave full permission)
Posted
Updated 13-Nov-12 1:56am
v4

maybe another program uses the files and then the above error occurs.
To see if you can have exclusive access to the file you can use this class:

C#
public class FileCheck
    {
        public static Boolean FileCanBeCopied(String FolderAndFileName)
        {
            Boolean canBeCopied = false;
            if (!String.IsNullOrEmpty(FolderAndFileName))
            {
                FileInfo fi = new FileInfo(FolderAndFileName);
                if (fi.Exists)
                {
                    FileStream stream = null;
                    try
                    {
                        // check if the file can be opened exclusive. If that is possible, then the file can be copied otherwise in transfer
                        using (stream = File.Open(FolderAndFileName, FileMode.Open, FileAccess.Write, FileShare.None))
                        {
                            if (stream.CanWrite)
                                canBeCopied = true;
                        }
                    }
                    catch (Exception)
                    {
                        canBeCopied = false;
                    }
                }
            }
            return canBeCopied;
        }
    }
 
Share this answer
 
if the access file is open, then it will lock the file, so try to close the access file before running your application,

some times it requires restarting your computer to make sure that no one else is using the access file with you,
 
Share this answer
 

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



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