Hi Guys,
I'm trying to delete previously Image from Image folder.
I want when data deleted from database same time image on folder deleted too.
I'm save image not using binary type data but just saving the image URL to database.
Value from database
~/FolderFotoAdmin/88259136_1769162196558696_4797795257829818368_n.jpg
Code bihind
protected void BtnDelete_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(koneksi))
{
using (SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete From tbl_user Where id_user ='" + edit_txtIDAdmin.Text.Trim() + "'";
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
//Delete previous Image in FolderFotoAdmin
LoadURLFromDb();
}
}
ShowData();
}
protected void LoadURLFromDb()
{
using (SqlConnection con = new SqlConnection(koneksi))
{
using (SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select url_foto From tbl_user Where id_user ='" + edit_txtIDAdmin.Text.Trim() + "'";
//code for Deleting previous Image in FolderFotoAdmin
string DeleteFile = Server.MapPath("~/FolderFotoAdmin/" + cmd) + Path.GetFileName(FileUploadFotoUser.PostedFile.FileName);
if (File.Exists(DeleteFile))
{
System.IO.File.Delete(DeleteFile);
}
cmd.ExecuteNonQuery();
con.Close();
cmd.Connection.Close();
}
}
}