Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
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:
|
Source File: c:\Users\XBlaZE\Desktop\Project-X\Test1\Members\Update.cshtml Line: 148
Stack Trace:
<div></div> |
I think oldfilename is null! thats the problem! But how does it pass through if(oldFileName!=null )condition?