Solved

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

Posted on 2013-11-01
8
838 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 

Author Comment

by:bobfike
ID: 39616770
emoreau:

How can I determine what account IIS is running under?
0
 
LVL 70

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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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