I was trying to add an image into a directory by using c#. I’m going to use this image to display in a gridview. I wrote the code but I’m getting an error and I couldn’t solve it. I have a directoy called images. This is where I’m getting error FileUpload1.SaveAs (Server.MapPath(“images/” + FileName). It says C:\Users\user\Documents\Visual Studio 2008\WebSites\WebSite1\images\im1.jpg’ part of path couldn’t be found. Here is my code:
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" + FileName));
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "INSERT INTO Books (Book_Name,Author_Name,FileName, FilePath, In_Lib) VALUES (@BN,@AN,@FileName, @FilePath,@LIB)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.AddWithValue("@BN", TextBox1.Text);
cmd.Parameters.AddWithValue("@AN", TextBox2.Text);
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "images/" + FileName);
cmd.Parameters.AddWithValue("@LIB", "YES");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
You are trying to save into a non existing directory.
You need to create the directory hierarchy before you can save to it.