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

I get Following error :

The Microsoft Jet database engine cannot open the file 'D:\AspProject\ExportToExcel\Web\UploadFiles'.  It is already opened exclusively by another user, or you need permission to view its data. 
Posted
Updated 25-Dec-11 18:24pm
v2

C#
string strFileType = System.IO.Path.GetExtension(filepath.ToLower());
                      string sSourceConstr = String.Empty;


                      if (strFileType.Trim() == ".xls")
                      {
                          sSourceConstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =" + filepath + "; Extended Properties=\"Excel 8.0; HDR=Yes; IMEX=2\"";
                      }
                      else if (strFileType.Trim() == ".xlsx")
                      {
                          sSourceConstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + filepath + "; Extended Properties=\"Excel 12.0; HDR=Yes; IMEX=2\"";
                      }


                      string sDestConstr = ConfigurationManager.ConnectionStrings["Class"].ConnectionString;
                      OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
                      using (sSourceConnection)
                      {
                          sSourceConnection.Open();

                          string sql = "select * from [" + dr["SheetName"].ToString() + "]"; //"select * from $",sheet
                          OleDbCommand command = new OleDbCommand(sql, sSourceConnection);

                          OleDbDataAdapter da = new OleDbDataAdapter();
                          da = new OleDbDataAdapter(command);
                          DataSet ds = new DataSet();
                          da.Fill(ds);
                          if (ds.Tables.Count > 0)
                          {
                              DataTable dt = ds.Tables[0];

                              if (dt.Rows.Count > 0)
                              {
                                  Users objUser = (Users)Session["UserSession"];
                                  string curUser = objUser.UserId;

                                  DataColumn dcUser = new DataColumn();
                                  dcUser.ColumnName = "CreatedBy";
                                  dcUser.DefaultValue = curUser;
                                  dt.Columns.Add(dcUser);

                                  DataColumn dcCreatedOn = new DataColumn();
                                  dcCreatedOn.ColumnName = "CreatedOn";
                                  dcCreatedOn.DataType = System.Type.GetType("System.DateTime");
                                  dcCreatedOn.DefaultValue = DateTime.Now;
                                  dt.Columns.Add(dcCreatedOn);

                                  int newsNo = dt.Rows.Count;
                                  string table = ("ZipUpload"+"_"+dr["SheetName"].ToString().Replace("$",""));
                                  string conString = ConfigurationManager.ConnectionStrings["Class"].ConnectionString;
                                  CommonBase objCommonBase = new CommonBase();
                                  objCommonBase.DeletTableData(table);

                                  FileUploadBase objFileUpload = new FileUploadBase();
                                  objFileUpload.BulkInsert(dt, table, conString);

                                  lblMessage.Text = "File uploaded successfully!  <br /> No. of Record: " + newsNo + "<br />";
                                  lblMessage.ForeColor = System.Drawing.Color.Green;




using This i solve My Problem Thank U Guys!
   
If you use MS Access or Ms Excel as database then their are 4 types of options are their

1.Open
2.Open Exclusively
3.Open Read Only
4.Open Read Only and Exclusively

While you are doing modification,insertion or deletion in the database
you should open in open mode only .

while you are doing these operations, if you already open the db or any other application use it exclusively or read only you cant access it, you will get this error.
   
my code is
C#
protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (fpExcel.FileName.ToString() != string.Empty)
        {
            string filepath = Server.MapPath("UploadFiles");
            fpExcel.SaveAs(filepath + "\\" + fpExcel.FileName.Split('\\')[fpExcel.FileName.Split('\\').Length - 1]);
            gvwReport.DataSource = GetExcelData(filepath);
            gvwReport.DataBind();
        }
    }

    private DataTable GetExcelData(string filepath)
    {
        string oledbConnectionString = string.Empty;
        OleDbConnection conn = null;
        oledbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + filepath + "; Extended Properties=Excel 8.0;";
        conn = new OleDbConnection(oledbConnectionString);
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }
        OleDbCommand command = new OleDbCommand("Select * from [Sheet1$]",conn);
      OleDbDataAdapter objAdapter = new OleDbDataAdapter();
      objAdapter.SelectCommand = command;
        DataSet objDataset = new DataSet();  
       objAdapter.Fill(objDataset, "ExcelDataTable");
       conn.Close();  
        return objDataset.Tables[0];  
    }

on opening the connection i get above error!
   
v2
Comments
Anuja Pawar Indore 1-Feb-12 4:24am
   
Use improve question link to paste your code there. This is answer section
[no name] 1-Feb-12 5:26am
   
ya i know this is answer section thats why i paste the answer.
Member 11342436 7-Feb-17 4:45am
   
You dont need to open connection for da

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