Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
i store a image path in a folder as well as database.so i need when a user delete a image from database same time image within folder should be delete.plz give me idea how to implement .
Posted
Updated 23-Feb-20 18:31pm
v2
Comments
bbirajdar 19-Nov-13 3:13am    
In the delete button click event , call two methods-
1. To delete image name from database
2. To delete image file from the disk.

Done.....
Member 12007795 6-Oct-15 9:34am    
I have one problem when I update the image then the earlier image would not deleted from the folder of the project give me solution on that

C#
foreach (DataListItem li in dlimagegallery.Items)
        {


            Image imagepath = li.FindControl("Image1") as Image;
            string path = Server.MapPath("imagepath");

            if (System.IO.File.Exists(path))
            {


                System.IO.File.Delete(path);
                Label1.Text = "File succefully deleted";

            }



Image find control does not find image Url.when i bind Label1.Text=imagepath then it show System.Web.UI.WebControls.Image.but this namespace i am already used. plz give me anyone soln.
 
Share this answer
 
Write a stored procedure, in that first store the image name in a variable and then delete the record from the database, and then return that variable value to the calling method, here check the value is valid or not, if valid then the return value is Path of the image, if not then surly the error has occured while deleting record. now you can delete the image file from the folder.
as like below...

System.IO.File.Delete(path);
 
Share this answer
 
Hi,
You can use SqlTransaction to delete the image from database under this transaction. Then delete the image from the folder.
I think you can use this code to delete image :

string filePath = Server.MapPath("~/" + PathDB);
if (System.IO.File.Exists(filePath))


{


System.IO.File.Delete(filePath);


}

Where PathDB contains the path from database.
I hope it will be helpful for you.
good luck
 
Share this answer
 
v3
Hi Mavendra,

Try this link[^].
Hope it will be very useful to you.

Regards,
RK
 
Share this answer
 
C#
if (delImage != "" && delImage != null)
                              {
                                  System.IO.File.Delete(Request.PhysicalApplicationPath + "Upload/" + delImage);
                              }



delimage - image name
 
Share this answer
 

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