I had uploaded a file in the database and that file is also contains by a folder of the website page in asp.net.
Now i want to delete the file from the database and the folder also. I have successfully deleted the
file from the database but file is not deleting from the folder of the website.
so tell me the proper answer.
The whole detail is given below:-
Firstly I had uploaded a file(whether it is image,word file etc.)Now u can
understand that file will also go to the folder .It mean to say that file will go the database(sql server) and also go to the folder of the website.
Now If i want to delete that file then it is deleting from the databse(sql server)
but not to deleting from the folder of the website(asp.net).Folder name like (image,upload etc.)
Here is my c# code for delete a file from the database and the folder also.The folder name is "Uploads" which you can see in the code and the databse table name is "emp" so now you can understand that file is deleting from the database table "emp" but not deleting from the folder "Uploads".
Code is given below:-
protected void btndelete_Click(object sender, EventArgs e)
{
string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
string filename = FileUpload1.FileName;
FileUpload1.PostedFile.Equals(Server.MapPath("~\\Uploads\\" + filename));
string path = "~\\Uploads\\" + filename;
SqlConnection cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "delete from emp ";
cmd.CommandType = CommandType.Text;
cmd.Connection = cnn;
cmd.ExecuteNonQuery();
if (File.Exists(path))
{
File.Delete(path);
}
cnn.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
lblinfo2.Text = "";
lblinfo2.Text= " Deleted Successfully ";
}
}