Click here to Skip to main content
15,878,970 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My code as follows

if (e.CommandName == "UploadFarmData")
{ 
string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);
Econ = new OleDbConnection(constr);
Econ.Open();
DataTable dtExcelSchema;
dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
da.SelectCommand = cmdExcel;
da.Fill(ds);
Econ.Close();
DataTable dt = new DataTable();
dt = ds.Tables[0];
bool s = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dal.DAL_Insert_Farm_Data(dt.Rows[i]["Farmer Code"].ToString(), dt.Rows[i]["Farm No"].ToString(),
dt.Rows[i]["Type of Farm"].ToString(), dt.Rows[i]["Seeded Date"].ToString(), dt.Rows[i]["Farm Visit No"].ToString());
s = true;
}

if (s)
{
showStatusTrue.Style.Add("display", "block");
showStatusTrue.InnerHtml = "Farm Details Uploaded Successfully";
dal.DAL_Data_Update();
}
 
when i upload shows error as follows
 
 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

What I have tried:

<pre>
My code as follows

<pre> 
 
if (e.CommandName == "UploadFarmData")
{ 
string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", CurrentFilePath);
Econ = new OleDbConnection(constr);
Econ.Open();
DataTable dtExcelSchema;
dtExcelSchema = Econ.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
DataSet ds = new DataSet();
string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
da.SelectCommand = cmdExcel;
da.Fill(ds);
Econ.Close();
DataTable dt = new DataTable();
dt = ds.Tables[0];
bool s = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
dal.DAL_Insert_Farm_Data(dt.Rows[i]["Farmer Code"].ToString(), dt.Rows[i]["Farm No"].ToString(),
dt.Rows[i]["Type of Farm"].ToString(), dt.Rows[i]["Seeded Date"].ToString(), dt.Rows[i]["Farm Visit No"].ToString());
s = true;
}

if (s)
{
showStatusTrue.Style.Add("display", "block");
showStatusTrue.InnerHtml = "Farm Details Uploaded Successfully";
dal.DAL_Data_Update();
}
 
when i upload shows error as follows
 
 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
Posted
Updated 7-Jun-18 19:40pm

1 solution

Quote:
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

If you read carefully the error message, you will understand that the problem is not in your code.
You are given the possible causes of problem, you can deduce actions to take, and we can't do them for you.
 
Share this answer
 

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