Click here to Skip to main content
16,018,797 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have an Excel sheet containing data and i want to upload that to the MYSQL database.
I have used FileUpload control to select the Excel file by the user.
While reading the data from Excel sheet the following error occuring at oledb.Open();

The Microsoft Office 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.

Below is my code:

 protected void btnImportcontacts_Click(object sender, EventArgs e)
    {
        string path =FileUploadContacts.PostedFile.FileName;
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+";Extended Properties=Excel 12.0";
        // Create the connection object
        OleDbConnection oledbConn = new OleDbConnection(connString);
        //try
        //{
          
            // Open connection
            oledbConn.Open();
           
 
            // Create OleDbCommand object and select data from worksheet Sheet1
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
 
            // Create new OleDbDataAdapter
            OleDbDataAdapter oleda = new OleDbDataAdapter();
 
            oleda.SelectCommand = cmd;
 
            // Create a DataSet which will hold the data extracted from the worksheet.
            DataSet ds = new DataSet();
 
            // Fill the DataSet from the data extracted from the worksheet.
            oleda.Fill(ds, "Customer");
            // Some more code here.....
           // code goes here....
Posted
Updated 22-Aug-13 22:04pm
v2
Comments
syed shanu 23-Aug-13 4:28am    
Check this link ,Might be permissions problem on ur Excel file.

http://www.aspdotnet-suresh.com/2013/01/c-microsoft-office-access-database.html

http://social.msdn.microsoft.com/Forums/office/en-US/7a46aa61-2a62-4a7a-a722-4a39b480638c/the-microsoft-jet-database-engine-cannot-open-the-file-it-is-already-opened-exclusively-by-another

1 solution

I have already tried.

I am able to execute, if i replace the data source like below;

This code
C#
string path =FileUploadContacts.PostedFile.FileName;
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+path+";Extended Properties=Excel 12.0";


as like

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Sample.xlsx;Extended Properties=Excel 12.0";


The problem is with the FileUpload Control.May be it is not taking the complete Path.
How can i resolve this?
 
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