Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-01
8
Medium Priority
?
868 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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