Solved

Read Excel in C# from a form in a web app

Posted on 2013-11-01
8
810 Views
Last Modified: 2013-11-07
I have written a web app using MVC 4 c#. I present to the user a form with an input type=file. This allows the user to browse folders for the excel file they want to upload. The filename with the entire path is returned to my controller which I use as the data source in the connection string.

System.Data.OleDb.OleDbConnection mCon;
            mCon = new System.Data.OleDb.OleDbConnection();
            mCon.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;data source=" + pathOfFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;READONLY=YES\";");

pathOfFile contains this:

G:\IT\EnterpriseApplications\Code\WebApps\FileUpload\LoadWells_Generwell.xlsx

I then do mCon.Open()

The G drive is a mapped network drive.

This works fine when I run it locally within Visual Studio. However, after I publish this web app to my server things change. I run the applicaion from my pc and when it gets to the part to open the connection I get this:

'G:\IT\EnterpriseApplications\Code\WebApps\FileUpload\LoadWells_Generwell.xlsx' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

Drive G on the server is also a mapped network drive and I can see the file using windows explorer on the server just fine.

What am I doing wrong?

Thanks
0
Comment
Question by:bobfike
  • 3
  • 3
  • 2
8 Comments
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39616628
This can be becauase of coupoe of reasons.

Are you on windows 7? if so,   one known reason for this issue is Account Control (UAC) is on

Try disabling it,


Aslo see here: http://support.microsoft.com/kb/207671/en-us
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39616664
Is it possible that the G drive is mapped on the server for your own account but IIS is not running under your account (like it should be)?

That means that you will need to map the drive for the IIS account. It is often easier to just give the full path instead of using mapped drives.
0
 
LVL 15

Expert Comment

by:unknown_routine
ID: 39616741
Yes it also a very likely.
0
 

Author Comment

by:bobfike
ID: 39616770
emoreau:

How can I determine what account IIS is running under?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39616876
you have to look in the Service applet. check http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/3648346f-e4f5-474b-86c7-5a86e85fa1ff.mspx?mfr=true

But I will say it again: consider using a full path (\\servername\sharename\...) instead of trying to change the permissions
0
 

Author Comment

by:bobfike
ID: 39621453
I changed the path to use the \\servername\sharename. Now I get this both locally and on the server:

Inner Exception Message:

Failure creating file.
StackTrace :

at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection) at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject) at System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.OleDb.OleDbConnection.Open() at GenerWellAdmin.Controllers.SpreadsheetUploadController.Validate(String file)
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 39621467
0
 

Author Closing Comment

by:bobfike
ID: 39631991
I did use a UNC path and selected a drive and path with permissions for everyone to read.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now