Click here to Skip to main content
12,891,469 members (50,676 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET

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,
   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);

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 13-Nov-12 1:50am
Updated 13-Nov-12 1:56am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:

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;
                        // 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;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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,

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

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100