The Microsoft Jet database engine cannot open the file. It is already opened exclusively by another user.

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.

I have already explain how to import Excel files using OLEDB, with extend to that topic if that file would be opened by some another user, you will be prompted “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.”

To resolve this problem, We have  to first check for whether this file is opened or not. To check that we could write code as below:

public bool FileInUse(string path)
{
    try
    {
        using (FileStream fs = new FileStream(path, FileMode.Open))
        { }
        return false;
    }
    catch (IOException ex)
    {
       return true;
    }
}

Whenever we call upload file we would call FileInUser Method inside that event or method as shown below:

protected void btnUpload_Click(object sender, EventArgs e)
{
    OleDbConnection conn = null;
    try
    {
        if (btnFileupload.HasFile == true)
        {
            string connString = "";
            string strFileType = Path.GetExtension(btnFileupload.FileName).ToLower();

            string path = btnFileupload.PostedFile.FileName;
            Extension = Path.GetExtension(path);
            Filename = Path.GetFileName(path);
            Byte[] bytes = btnFileupload.FileBytes;
               
            //Connection String for Excel Workbook
            if (strFileType.Trim() == ".xls")
            {
                connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;ReadOnly=true;HDR=Yes;IMEX=2\"";
            }
            else if (strFileType.Trim() == ".xlsx")
            {
                connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;ReadOnly=true;IMEX=2\"";
            }
            string query = "SELECT * FROM [Entry$]";

            if (FileInUse(path))
            {
                Page page = HttpContext.Current.CurrentHandler as Page;
                string script = "<script type=\"text/javascript\">alert('File Already In Use and Open.');</script>";
                page.ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", script);
                return;
            }

            conn = new OleDbConnection(connString);
            if (conn.State == ConnectionState.Closed) conn.Open();

            using (OleDbCommand cmd = new OleDbCommand(query, conn))
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
                {
                    using (DataSet ds = new DataSet())
                    {
                        da.Fill(ds);
                        if (ds.Tables.Count > 0)
                        {
                            btnSubmitToAlis.Visible = true;
                            btncancel.Visible = true;
                            trDate.Visible = true;
                            GrdExcelData.DataSource = ds.Tables[0];
                            lbltotalRecord.Text = Convert.ToString(ds.Tables[0].Rows.Count);
                            trlblmsg.Visible = true;
                            GrdExcelData.DataBind();
                        }
                    }
                }
            }
        }
        else
        {
            GrdExcelData.DataSource = null;
            GrdExcelData.DataBind();
        }
    }
    catch (Exception ex)
    {}
    finally
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        conn.Dispose();
    }
}

That’s all how to find whether file is opened or not.

 

 

Leave a Reply