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

checking username and password from a ms access database
Xamarin Android RunOnUiThread() not working after new OnCreate() call
Getting Error Unable to evaluate expression in Azure Web Application Application
How is Task.Run limited by CPU cores?
Mixing TPT and TPC Inheritance in Entity Framework
Update updatepanel from another class
Use SQL geography field to initialize .NET DbGeography object [closed]
How to correctly receive data via serial port?
Property or indexer 'Font.Bold' can not be assigned to - it is read only [duplicate]
C# - Prevent animation of a showing form
Silverlight how to bind properties to List item values
How do I rotate all graphics in form without causing MouseArgs to go haywire
Create PivotChart in excel genereation
How to send variables from Main to another method, then return a value back to Main method?
Can not detect USB 3.0 port information with c# using DeviceIoControl
Operators.NotObject VB to C#

Categories

HOME
google-cloud-pubsub
twitter-bootstrap
network-programming
blob
opencv4android
arcgis
jquery-plugins
spss
where
pandoc
uml-designer
opacity
flowtype
procmon
kohana
open-search-server
pip
sahi
ethereum
ng2-charts
responsive
k-means
conditional-formatting
computation-theory
polybase
pumping-lemma
functional-dependencies
apdu
ppp
expressionengine
cloudformation
scripting-language
typeclass
r-factor
edirectory
urhosharp
spring-oauth2
snapkit
jsonapi-resources
apache-falcon
redhat-brms
area
ng-lightning
glide-image-library
irr
paperclip
encoder
jks
activeandroid
skype-bots
runtime-permissions
bioperl
openpgp.js
reloaddata
dynatable
node.js-client
iban
apache-commons-cli
stripes
gevent
networkstream
scriptengine
magick.net
android-xmlpullparser
django-taggit
paypal-subscriptions
gtkwave
database-project
transbase
nmock
firewire
page-refresh
radgrid
beagleboard
strcpy
wingdb
conkeror
fiware-health
cordova-3
launcher
behance-api
debuggervisualizer
carrier
node-amqp
azure-pack
panelgrid
system.transactions
yii-components
volatility
linear-interpolation
esi
dancer
django-admin-tools
diazo
rjs
openwrap
service-layer
xcdatamodel
mysql-logic
mysql-pconnect
dgml
atmega16
expander
httpbrowsercapabilities

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