Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - e: Is a Physical Path but a Virtual Path was Expected in Asp.net

Oct 6, 2015
Introduction:

Here I will explain how to solve problem of “e: is a physical path but a virtual path was expected” in asp.net using c#, vb.net. Generally this physical and virtual path problem occurred whenever we refer “Server.MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.MapPath only once while getting or inserting files in folder in asp.net using c#, vb.net.

Description:


In previous posts I explained export gridview data to excel in windows application, auto suggestions search textbox in master page in asp.net, download multiple files as zip folder in asp.net and many articles relating to solve errors in asp.net, SQL Server, IIS, etc. Now I will explain how to solve problem ofe: is a physical path but a virtual path was expected” in asp.net using c#, vb.net.

When I run following code I got error like physical path but a virtual path was expected in asp.net using c#, vb.net application


HttpPostedFile file = files[i];
string fname;
fname = file.FileName;
string strpath =Server.MapPath("~/images/") + tutorial;
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
}
DataSet _ds = new DataSet();
_ds = CommonMethods.Getdataset("insertimagedetails", "insertedimages");
if (!string.IsNullOrEmpty(_ds.Tables[0].Rows[0][0].ToString()))
{
imgids += _ds.Tables[0].Rows[0][0].ToString()+",";
fname = Path.Combine(Server.MapPath(strpath), fname);
file.SaveAs(fname);
}

When I run above code I got problem like “physical path but a virtual path was expected in asp.net” because I already got physical path for strpath parameter initial itself again while saving in folder I am trying to get physical path from already available path that’s why this problem occurred.

To fix this problem we need to use only one physical path for that we need to remove “Server.MapPath” from folder path like as shown below


HttpPostedFile file = files[i];
string fname;
fname = file.FileName;
string strpath =Server.MapPath("~/images/") + tutorial;
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
}
DataSet _ds = new DataSet();
_ds = CommonMethods.Getdataset("insertimagedetails", "insertedimages");
if (!string.IsNullOrEmpty(_ds.Tables[0].Rows[0][0].ToString()))
{
imgids += _ds.Tables[0].Rows[0][0].ToString()+",";
fname = Path.Combine(strpath, fname);
file.SaveAs(fname);
}

I hope it helps you to fix your “e: is a physical path but virtual path was expected” in c#, vb.net. Happy Coding……

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

2 comments :

Unknown said...

hi sir,
im facing the same problem can u pls send full code

Unknown said...

Super Sir

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.