Hi all .
i have 2 buttons one of them upload an image and another delete it .
In my code there is an <asp:image> also which as soon as upload image asp:image show that image and after deleting image should disappear from the form .
my add button has the below code :
protected void Btn_upload_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlCommand cmdInsert = new SqlCommand("insert into tbl_Img (Fld_Uploader,Fld_path,Fld_FileName) values ('Admin','" + PublicClass.ImgPath() + "','" + File_Upload.FileName + "')", con);
cmdInsert.ExecuteNonQuery();
SqlCommand cmdGetId = new SqlCommand("select top 1 * from tbl_img order by fld_id desc", con);
SqlDataAdapter da = new SqlDataAdapter(cmdGetId);
DataTable dt = new DataTable();
da.Fill(dt);
ImgID = dt.Rows[0]["Fld_id"].ToString();
RandCode = dt.Rows[0]["Fld_RandCode"].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
try
{
ImgPath = Server.MapPath(PublicClass.ImgPath() + ImgID + "_" + File_Upload.FileName);
File_Upload.SaveAs(ImgPath);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Img.ImageUrl = "../../ReadAttachment.aspx?code=" + RandCode + "&maxWidth=100&maxHeight=100";
BtnDelete.Visible = true;
btnAdd.Enabled = true;
}
and my Delete btn has this code :
protected void BtnDelete_Click(object sender, EventArgs e)
{
try
{
if (System.IO.File.Exists(ImgPath))
{
Img.ImageUrl = "";
System.IO.File.Delete(ImgPath);
Response.Write("Deleted");
}
else
{
Response.Write("its not exist");
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
but at delete button i get this error :
The process cannot access the file 'L:\chikardarin Website\WebApplication2\image_logo\11_nissan-cars-logo-emblem.jpg' because it is being used by another process.
note : this example is run on localHost .
please tell me how to discard my image-url at delete time . thank you :)