c#


Exception thrown when opening a Excel spreedsheet from C# stating file is being used by another user


I am currently developing an application that requires an excel spreadsheet, location selected by the user, to be read into a DataTable and then stored in a sql server database.
The application works perfectly in my development environment, however when it is deployed into my production environment an exception is thrown with the following message.
The Microsoft Jet database engine cannot open the file '.xls'. It is already opened exclusively by another user, or you need permission to view its data.
My code to read the excel file is as follows:
OleDbConnection objConn = null;
DataSet objDataset1 = null;
string fileLocation = GetFileLocation();
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=" + fileLocation
+ ";" + "Extended Properties=Excel 8.0;";
objConn = new OleDbConnection(sConnectionString);
objConn.Open(); //This is where the exception is thrown
OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
objDataset1 = new DataSet();
objAdapter1.Fill(objDataset1, "XLData");
DataTable dt = objDataset1.Tables["XLData"];
Note the application and excel file are on different servers in the same domain.
Digging around various forumns and knowledge bases it would appear that the exception is thrown when the "user" does not have permission to use the file. Although not recommended permissions on the file have been set to Full Access for all users.
Apart from file permissions what else could cause this exception to be thrown?
Not sure if this is helpful but...
The error:
The Microsoft Jet database engine
cannot open the file '.xls'.
seems to point to a file named .xls, as opposed to something named, say myspreadsheet.xls or prodfile.xls.
It may be a long shot, but can you do some debugging to make sure that the file Jet is trying to open actually exists? It may be that the filename is not being constructed properly, for some reason.
Here's a few answers to the same root problem:
Read from Excel using OleDb in a Windows Service?
kill the previous opened excel process before starting the new process for reading the excel.it will solve your problem.
i wrote one function you can call it before opening the xls.
private void kill_excel_process()
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
}

Related Links

How to add new items in Gridview through POPUP WINDOW
how to avoid open page already exist on Navigation stack? [duplicate]
Create Entity Framework model based on an existing database in ASP.NET Core
Rotate image in a button
WPF: Updating Database (TwoWay) doesn´t work with Linq-Query
c# How to avoid child form to inherit his mdi parent WindowState property?
Using type FileInfo in WPF XAML
Password encryption in PHP equivalent to C#
Is it possible that a C# method parameter of some kind of int can accept null and can update argument
How to dynamically merge a reactivelist of observables into one observable in ReactiveUI, C#
Cannot implicitly convert type 'app.a1<<string>>' to 'app.a1<<object>>'
What exactly are the resources used when querying a database and why do they need disposing?
RegularExpressionValidator ErrorMessage With Border Overflowing Across Two Lines
How to make custom date validation attribute?
C# Stack Array, Push method pushes the value to ALL entries
Entity Framework Core is slow - how do I make it faster?

Categories

HOME
asterisk
oop
model-view-controller
primefaces
typo3
webvr
ibm
skypedeveloper
mifare
django-middleware
crm
grid
glympse
aggregation-framework
autocad
obd-ii
dcos
rename
activejdbc
google-search-appliance
guidewire
ng2-charts
drush
cakephp-2.0
derby
yui
webseal
infopath
css-float
ofbiz
summernote
restructuredtext
formulas
smali
ps
utc
boosting
dynamic-memory-allocation
semantic-ui-react
schedule
keil
choco
android-scrollview
dropdownbox
odp.net
yelp
iterm2
lex
scalamock
file-conversion
google-cloud-powershell
irr
binary-decision-diagram
peerjs
head
gridgain
jenkins-2
pyopenssl
activesupport
robotc
scanf
sqlproj
timefield
scikit-image
papyrus
instaparse
angularfire
system-integration
sbt-proguard
python-rq
usps
mod
web-optimization
firefox-os
fadein
markerspiderfier
gnu-sort
serial-communication
opennebula
gcc-warning
mri
teamstudio-unplugged
random-access
register-allocation
nsnotificationcenter
powerpoint-2010
visual-studio-setup-proje
yandex-api
pidgin
data-layers
nomachine
imaplib
google-mirror-api
mencoder
kendo-window
angularjs-select2
stringstream
uikit-dynamics
proj4
auctex
biztalk-deployment
boost.build
tasklist
jmesa
eeprom
helicontech
nsundomanager
log-shipping
xpolog
sipdroid
jqtransform
norton
marathontesting
llblgen
mediastreamsource
subdirectories
chromebug
database-agnostic
atmega16
ihtmldocument2
disk-based

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App