Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had uploaded a file in the database and that file is also contains by a folder of the website page in asp.net.
Now i want to delete the file from the database and the folder also. I have successfully deleted the
file from the database but file is not deleting from the folder of the website.
so tell me the proper answer.
The whole detail is given below:-

Firstly I had uploaded a file(whether it is image,word file etc.)Now u can
understand that file will also go to the folder .It mean to say that file will go the database(sql server) and also go to the folder of the website.

Here is my c# code for delete a file from the database and the folder also.The folder name is "Uploads" which you can see in the code and the store procedure is "PR_Brand_DeleteByPK"; so now you can understand that file is deleting from the database. but not deleting from the folder "Uploads".

Code is given below:-
C#
protected void gvBrand_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
            objConn.Open();

            SqlCommand objCmd = objConn.CreateCommand();
            objCmd.CommandType = CommandType.StoredProcedure;

            objCmd.CommandText = "PR_Brand_DeleteByPK";
            objCmd.Parameters.AddWithValue("@BrandID", Convert.ToInt32(e.CommandArgument.ToString()));

            
            FileInfo path = new FileInfo(Server.MapPath("~/Upload/filename" ));
            path.Delete(); // i try this//

            
            objCmd.ExecuteNonQuery();
            gvBrand.DataBind();
        }
    }
Posted
Updated 18-Mar-13 0:36am
v2
Comments
Zoltán Zörgő 18-Mar-13 6:40am    
And the exception you get is....?
Ankit Gajera 18-Mar-13 6:56am    
Exception is not generated,only image is not delete from the folder.i think require physical path ?? bt how can i apply??

It's not working for two reasons:
Firstly, "Uploads" is not the same as "Upload" - your text says the folder is "Uploads" and the code shows "Upload" instead.
Secondly, because it is unlikely that the file you want to remove is called "filename" - I suspect that you need to incorporate the file name into the string before you call the Server.MapPath method.

Once you fix them, try again - FileInfo.Delete should work. If it doesn't, then check the file name you are trying to remove manually against the value you get back from Server.MapPath.
 
Share this answer
 
hi!!! i m written this code for delete pic from folder
take a look of this it should helps you

C#
protected void DelBtn_Click(object sender, EventArgs e)
       {

           if (ListBox1.SelectedIndex== 0)
           {
               Itemessage.Visible = true;
               Itemessage.Text = "plz select another Image from box it is main pic ..";
               return;

           }


       if( ListBox1.SelectedItem==null)
       {
           Itemessage.Visible = true;
           Itemessage.Text = "plz select Image from box first..";
           return;

       }
           if (ListBox1.SelectedIndex > -1)
           {
               string _value = ListBox1.SelectedItem.Value; //Gets the value of items in list.
               string _text = ListBox1.SelectedItem.Text;  // Gets the Text of items in the list.
               ListItem item = new ListItem(); //create a list item
               item.Text = _text;               //Assign the values to list item
               item.Value = _value;
               string completePath = Server.MapPath("~/ItemsPics/" + item.Text);

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

                   System.IO.File.Delete(completePath);
                }
               ListBox1.Items.Remove(item); //Remove from the selected list
               searchimg();
           }

      }
 
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