Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-11-01
8
Medium Priority
?
885 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 2000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

824 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