Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi when i want delete my image from folder my code not work and make error
anybody can help me??
C#
SqlConnection con = new SqlConnection(connection.connectionStr);
       SqlDataAdapter da=new SqlDataAdapter ("select productImage from companyCategory where productCategoryID='" + txtCode.Text.Trim() + "'",con);
       DataTable dt = new DataTable();
       da.Fill(dt);
       string str = dt.Rows[0][0].ToString();
       System.IO.File.Delete(Server.MapPath("all_images/Gallery/smallImage") + "/" + str);
       System.IO.File.Delete(Server.MapPath("all_images/Gallery") + "/" + str);


CSS
it error is:
Could not find a part of the path 'C:\Documents and Settings\smm\Desktop\ESFAHAN_WOODS.COM\admin_admin\all_images\Gallery\smallImage\1.jpg'.

but why my path is invalid????
Posted
Updated 21-Sep-11 20:31pm
v3
Comments
Prerak Patel 22-Sep-11 2:17am    
What is the exact error?

Are we to guess what error you receive?!

Well ok, my guess is your path is invalid.

Btw, what is the prize for a correct guess?
 
Share this answer
 
First off, don't do it like that: use a parametrized query instead:
C#
SqlDataAdapter da=new SqlDataAdapter ("select productImage from companyCategory where productCategoryID=@PC", con);
da.SelectCommand.Parameters.AddWithValue("@PC", txtCode.Text.Trim());
The way you are working leaves you wide open to SQL injection attacks which can accidentally or deliberately destroy your database.

Secondly, check your table contains data - what happens if the user types the name wrong? Bang! "Object not set to an instance..." error and your code falls over. Blindly using "[0][0]" without checking is a stupid, pointless waste of user time.

Thirdly, check you data coming back from the DB - Since I don't know what your table looks like, I can't be sure, but there is a good chance that dt.Rows[0][0].ToString() is returning a datatype name, rather than a file name.


Finally, beware of relative paths! If your page is not in your root, (and it probably shouldn't be) then you need to look at prefixing your path with "~/" to specify exactly the path to your folders.
 
Share this answer
 
it error is:
Could not find a part of the path 'C:\Documents and Settings\smm\Desktop\ESFAHAN_WOODS.COM\admin_admin\all_images\Gallery\smallImage\1.jpg'.

but why my path is invalid????
 
Share this answer
 
now it error improved but my image not delete
C#
System.IO.File.Delete(Server.MapPath("~/all_images/Gallery/smallImage") + "/" + str);
        System.IO.File.Delete(Server.MapPath("~/all_images/Gallery") + "/" + str);
 
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