Uni Soh

Uni Soh

  • NA
  • 1
  • 4.6k

System.IO.DirectoryNotFoundException

Aug 10 2014 3:57 PM

I am using c# asp.net and I want to save the image path into database, but when I clicked the post button, it gave me this error. Kindly advise. Thank you.
 System.IO.DirectoryNotFoundException: Could not find a part of the path 
Line 30: FileUploadControl.SaveAs(Server.MapPath("Images").ToString() + @"\" + FileUploadControl.FileName);
Line 31:pbox.ImageUrl = @"~\Images\" + FileUploadControl.FileName;

Stack Trace:
[DirectoryNotFoundException: Could not find a part of the path 'C:\Users\uninvited\Downloads\ShareSpace(1)\ShareSpace\ShareSpace\Images\'.] System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +359 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1305 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) +60 System.IO.FileStream..ctor(String path, FileMode mode) +55 System.Web.HttpPostedFile.SaveAs(String filename) +94 System.Web.UI.WebControls.FileUpload.SaveAs(String filename) +23 ShareSpace.posting.Page_Load(Object sender, EventArgs e) in c:\Users\uninvited\Downloads\ShareSpace(1)\ShareSpace\sharespace\posting.aspx.cs:30 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51 System.Web.UI.Control.OnLoad(EventArgs e) +92 System.Web.UI.Control.LoadRecursive() +54 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

----------------------------------------------------------------------------------------

protected void Page_Load(object sender, EventArgs e)

{

if (FileUploadControl.HasFile)
{
String filextn = System.IO.Path.GetExtension(FileUploadControl.FileName).ToLower();
String[] filter = { ".gif", ".png", "jpg" };

pbox.Visible = true;
tbContent.Visible = false;
lblContent.Visible = false;

}

FileUploadControl.SaveAs(Server.MapPath("Images").ToString() + @"\" + FileUploadControl.FileName);
pbox.ImageUrl = @"~\Images\" + FileUploadControl.FileName;
}
protected void btnPost_Click(object sender, EventArgs e)
{
AddPost newPost = new AddPost();
bool addSuccess = newPost.AddPostt(tbTitle.Text, tbContent.Text, ddlCategory.SelectedIndex);

if (addSuccess)
{
Response.Write("Successfully submitted post!");
}
else
{

newPost.AddPicPostt(tbTitle.Text, FileUploadControl.FileName, ddlCategory.SelectedIndex);
Response.Write("Successfully submitted image");
}

}
 --------------------------------------------------------------------------------------------------------------------------------------------------
  public bool AddPostt(string title, string content, int category)
{
var newPost = new post();
newPost.accountID = 1;
newPost.postTitle = title;
newPost.postContent = content;
newPost.postDateTime = DateTime.Now;
newPost.categoryID = category + 1;

if (newPost.postTitle.Length < 1 || newPost.postContent.Length < 1)
{
return false;
}

using (DatabaseContext _db = new DatabaseContext())
{
_db.posts.Add(newPost);
_db.Configuration.ValidateOnSaveEnabled = false;
_db.SaveChanges();
}
return true;
}
public bool AddPicPostt(string title, string mediaPath, int category)
{
var newPost = new post();
newPost.accountID = 1;
newPost.postTitle = title;
newPost.mediaPath = mediaPath;
newPost.postDateTime = DateTime.Now;
newPost.categoryID = category + 1;

if (newPost.postTitle.Length < 1 || newPost.mediaPath.Length < 1)
{
return false;
}

using (DatabaseContext _db = new DatabaseContext())
{
_db.posts.Add(newPost);
_db.Configuration.ValidateOnSaveEnabled = false;
_db.SaveChanges();
}
return true;
}
}

Answers (1)