Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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,
   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 13-Nov-12 1:50am
N!dh!sh690
Edited 13-Nov-12 1:56am
v4
Rate this: bad
good
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;
                    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;
        }
    }
  Permalink  
Rate this: bad
good
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,
  Permalink  

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



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