Solved

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

Posted on 2013-11-01
8
800 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video discusses moving either the default database or any database to a new volume.

708 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

14 Experts available now in Live!

Get 1:1 Help Now