title: The Microsoft Office Access database engine cannot open or write to the file ''
body: <p>Data needs to be read into .net gridview from excel worksheet.
  Here are the aspx and aspx.cs codes.</p>
  
  <p>ASPX:</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;title&gt;Read and Display Data From an Excel File (.xsl or .xlsx) in ASP.NET&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" runat="server"&gt;
  &lt;div&gt;
  &lt;b&gt;Please Select Excel File: &lt;/b&gt;
  &lt;asp:FileUpload ID="fileuploadExcel" runat="server" /&gt;&amp;nbsp;&amp;nbsp;
  &lt;asp:Button ID="btnImport" runat="server" Text="Import Data" OnClick="btnImport_Click" /&gt;
  &lt;br /&gt;
  &lt;asp:Label ID="lblMessage" runat="server" Visible="False" Font-Bold="True" ForeColor="#009933"&gt;&lt;/asp:Label&gt;&lt;br /&gt;
  &lt;asp:GridView ID="grvExcelData" runat="server"&gt;
  &lt;HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" /&gt;
  &lt;/asp:GridView&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ASPX.CS</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Data.OleDb;
  using System.IO;
  
  public partial class _Default : System.Web.UI.Page
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  
  }
  protected void btnImport_Click(object sender, EventArgs e)
  {
      string connString = "";
      string strFileType = Path.GetExtension(fileuploadExcel.FileName).ToLower();
      string path = fileuploadExcel.PostedFile.FileName;
      //Connection String to Excel Workbook
      if (strFileType.Trim() == ".xls")
      {
          connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
      }
      else if (strFileType.Trim() == ".xlsx")
      {
          connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path +";Extended Properties=\"Excel 12.0 Xml;HDR=Yes;IMEX=2;\"";
      }
  
      string query = "SELECT [UserName],[Education],[Location] FROM [Sheet1$]";
      OleDbConnection conn = new OleDbConnection(connString);
          conn.Open();
      OleDbCommand cmd = new OleDbCommand(query, conn);
      OleDbDataAdapter da = new OleDbDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      grvExcelData.DataSource = ds.Tables[0];
      grvExcelData.DataBind();
      da.Dispose();
      conn.Close();
      conn.Dispose();
  }
  </code></pre>
  
  <p>conn.Open(); gives the following error:</p>
  
  <blockquote>
    <p><em>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.</em></p>
  </blockquote>
  
  <p>Network service has been provided all the permissions for the folder in which the excel sheet is stored.</p>