Click here to Skip to main content
15,885,899 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm working on a simple Insert, Fetch Web application. While inserting data I'm saving Images to the Folder "~/Photos/" and it's path in Database, But getting Error:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\Vipin\Documents\Visual Studio 2010\WebSites\NNSEnrollment\Photos\'.

The Folder is at its defined directory.
My Code is:
try
{
string filename = FileUpload1.FileName;

FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\Photos\\" + filename.Trim()));

string path = "~\\Photos\\" + filename.Trim();


str = "insert into spouse_details (r_p_id,name,dob,contact,marriage_anniversary_date,image_path) values('" + Convert.ToInt16(lbl_r_p_id.Text) + "','" + txt_name_spouse.Text + "','" + Convert.ToDateTime(txt_sp_dob.Text) + "','" + txt_contact.Text + "','" + Convert.ToDateTime(txt_anniversary.Text) + "','" + path + "')";
dap = new OleDbDataAdapter(str, con);
dt = new DataTable();
dap.Fill(dt);
Response.Write("<script>alert('Submit');</script>");
}

catch (Exception ex)
{
Response.Write(ex.ToString());
}

Whereas the same code is working in Some other application earlier... Thank You in Advance
Posted

1 solution

So check the path and see if the folder is there.
My guess is that you got the name wrong when you created the "Photos" folder in the root of your website.

Oh and do yourself a favour: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Particularly with a website, it's extremely dangerous...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900