Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am installed Microsoft AccessDatabaseEngine 2010. after that i dont do anything...because i dont know anything about oledb connection...... Then i am execute the below file then i am getting the following error... The Microsoft 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... ..... How can i solve that... Anyone help me please...If any steps are involved in oledb connection then show that steps please ..........

Aspx file :
XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="UploadExcel_to_DB.aspx.cs" Inherits="UploadExcel_to_DB" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <p>
        <br />
    </p>
<div>
<table>
<tr>
<td>
<span style="color: Red">*</span>Attach Excel file
</td>
<td>
<asp:FileUpload ID="fileuploadExcel" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSend" runat="server" Text="Export" onclick="btnSend_Click"  />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="4"
        ForeColor="#333333" GridLines="None" PageSize="30">
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
    <p>
    </p>
    <p>
    </p>
    <p>
    </p>
</asp:Content>

Aspx.cs File :
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class UploadExcel_to_DB : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        String strConnection = "Data Source=(local);Initial Catalog=MyDB;Integrated Security=True";
        //file upload path
        string path = fileuploadExcel.PostedFile.FileName;
        //Create connection string to Excel work book
        string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
        //Create Connection to Excel work book
        OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
        //Create OleDbCommand to fetch data from Excel
        OleDbCommand cmd = new OleDbCommand("Select [Business_ Name],[Category] from [Sheet1$]", excelConnection);
        excelConnection.Open();
        OleDbDataReader dReader;
        dReader = cmd.ExecuteReader();
        SqlBulkCopy sqlBulk = new SqlBulkCopy(strConnection);
        //Give your Destination table name
        sqlBulk.DestinationTableName = "exportcheckTB";
        sqlBulk.WriteToServer(dReader);
        excelConnection.Close();
    }
}

Web.config file :
XML
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <connectionstrings>
    <add name="ApplicationServices">
         connectionString="data source=Data Source=(local);Initial Catalog=MyDB;Integrated Security=True"
         providerName="System.Data.SqlClient" /&gt;
  </add></connectionstrings>

  <system.web>
    <compilation debug="true" targetframework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionstringname="ApplicationServices">
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </add></providers>
    </membership>

    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionstringname="ApplicationServices" applicationname="/" />
      </providers>
    </profile>

    <rolemanager enabled="false">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionstringname="ApplicationServices" applicationname="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationname="/" />
      </providers>
    </rolemanager>

  </system.web>

  <system.webserver>
     <modules runallmanagedmodulesforallrequests="true" />
  </system.webserver>
</configuration>
Posted
Updated 5-Sep-17 2:08am
v2
Comments
ZurdoDev 15-Mar-13 11:42am    
The error tells you what is happening. Someone has the file open.

1 solution

For your own sake, stop using access databases in multiuser environments. Look to use SQL server, or MySql instead - they are designed for multiuser operation in ways that access files are most definitely not.

When you use Access files directly in a website, you are running the risk that any other user can be trying to modify the file at the same time - and as a result you get an error. In your code it is even worse, because you are not disposing of the connection to the access file at all, so it will remain open until the garbage collector gets kicked in, or the session expires, whichever comes first.
 
Share this answer
 
Comments
SnvMohan 15-Mar-13 13:00pm    
Hello sir, i am using SQL server only for database.... I need to export the excel values into database-table....... How can i do that...?
Maciej Los 15-Mar-13 16:21pm    
Absolutly right!
+5

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