Share via


Deleting existing image after uploading new image!

Question

Saturday, December 29, 2012 3:37 PM

How do i Delete current image if a user updates a new image!

All replies (6)

Sunday, December 30, 2012 7:44 AM ✅Answered

Solved

http://forums.asp.net/t/1869499.aspx/1?Illegal+characters+in+path+

and please check

http://forums.asp.net/t/1160333.aspx/1


Sunday, December 30, 2012 12:58 AM

Hi

You can do like this.

//Code for Deleting from database
SqlCommand cmd = new SqlCommand(“Delete from tblEmp where Id=@Id”, con);
cmd.Parameters.AddWithValue(“@Id”, id1);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
fillgrid();

//code for Deleting Image in folder
string fPath = ViewState["ImgUrl"].ToString();
string fPath4 = Server.MapPath(fPath);

System.IO.File.Delete(fPath4);

Sunday, December 30, 2012 1:25 AM

Why don't you simply overwrite the image


Sunday, December 30, 2012 2:00 AM

You can Update image to database by user

http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Insert-Update-Image-To-SQL-Server.html

and delete folder then save your image.

http://stackoverflow.com/questions/5650718/delete-file-vs-directory-recreate-performance

http://stackoverflow.com/questions/5438930/image-in-current-folder-not-deleted-upon-new-upload

Good luck..


Sunday, December 30, 2012 3:57 AM

Go through this

First: Upload a new image with the data you need from the user.

After that the main thing. DELETING THE PREVIOUS IMAGE

There you will need a database string, that will have the value of the name of the previous image as a string. Than you will use the file deletion method to delete the whole path of the image (This will only delete image, not the whole folder)

You can learn this here!

http://www.asp.net/web-pages/tutorials/files,-images,-and-media/working-with-files


Sunday, December 30, 2012 5:28 AM

Did this code. But what if Database doesnt contain any image names? I get an error when database is empty! I tried the following code! It works fine if there is already an image uploaded, but if it is not then i get error!

if(Validation.IsValid()){   
 
            var SQLselect = "Select ImageName FROM Info WHERE UserId=@0";  //Select oldFilename to delete old image.
            var cuname = db.QueryValue(SQLselect,Id);
            
            oldFileName = cuname;
            
            var updateCommand = "UPDATE Info SET FirstName=@0,LastName=@1, Institution=@2, Course=@3,AdmissionYear=@4,PassOutYear=@5,DateOfBirth=@6,CurrentLocation=@7,CurrentOrg=@8,Phone=@9,Mobile=@10,ImageName=@11 WHERE UserId=@12";
            db.Execute(updateCommand, fname, lname,institu, Course, admission,passOut,birthdate,cuLoc,cuOrg, phone, mobile,newFileName,Id);
 
            //Image Delete
             if(oldFileName!=null && oldFileName!= newFileName){ 
               if(File.Exists(Server.MapPath("~/Images/" + oldFileName))){
                File.Delete(Server.MapPath("~/Images/"+ oldFileName));
            }
        }

Error:

Illegal characters in path.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Illegal characters in path.

Source Error: 
Line 146:            //Image Delete
Line 147:             if(oldFileName!=null && oldFileName!= newFileName){ 
Line 148: if(File.Exists(Server.MapPath("~/Images/" + oldFileName))){ Line 149:                File.Delete(Server.MapPath("~/Images/"+ oldFileName));
Line 150:            }
Source File: c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml    Line: 148 

Stack Trace: 
[ArgumentException: Illegal characters in path.]
   System.IO.Path.CheckInvalidPathChars(String path) +126
   System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str) +54
   System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList) +73
   System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path) +63
   System.Web.InternalSecurityPermissions.PathDiscovery(String path) +29
   System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping) +146
   System.Web.HttpRequest.MapPath(VirtualPath virtualPath) +37
   System.Web.HttpServerUtility.MapPath(String path) +99
   System.Web.HttpServerUtilityWrapper.MapPath(String path) +11
   ASP._Page_Members_Update_cshtml.Execute() in c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml:148
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119
<div></div>
I think oldfilename is null! thats the problem! But how does it pass through if(oldFileName!=null )condition?