Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:


I,m using this code for import file Excel

string f = fu.PostedFile.FileName;
string fileType = f.Split('.')[1];

OleDbConnection excelCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + f + ";Extended Properties=Excel 8.0");

if (excelCon.State == ConnectionState.Closed)
{
excelCon.Open();
}

OleDbCommand excelCom = new OleDbCommand("SELECT * FROM [Sheet1$]", excelCon);
OleDbDataAdapter excelAdp = new OleDbDataAdapter(excelCom);
DataSet excelDs = new DataSet();
excelAdp.Fill(excelDs);

gvSending.DataSource = excelDs.Tables[0];
gvSending.DataBind();

excelCon.Close();
Posted

1 solution

The error message is pretty explicit:
The Microsoft Jet database engine cannot open the file ''. It is already opened exclusively by another user, or you need permission to view its data.

It means exactly what it says. Either the file is open from a different (or even the same) application, or the file access permissions do not include Read access for the user that runs the code.

You need to look at whatever else is opening the file, and / or check the folder permissions.

[edit]
In fact, the problem is probably simpler than that: you can't open a file that doesn't exist: and the file won't exist if it's name is blank, and probably won't exist in the current web site folder if you are downloading it - check that you have saved in in an appropriate folder as well and then use the path to that file in your code.
[/edit]
 
Share this answer
 
v2

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


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