Itsme

Itsme

  • NA
  • 107
  • 5.4k

Delete image from folder

Jul 24 2019 10:28 PM
I am trying to perform curd operation ...everything is working fine but when I delete a entry from gridview ...it is being deleted successfully but it is not being deleted from folder ..where all the images are beingstored.Actually i dont have file name so how can i delete image from folder please suggest.
designer page:
  1. <asp:GridView runat="server" ID="gvImage" AutoGenerateColumns="false" CssClass="mydatagrid"  
  2. OnRowCancelingEdit="gvImage_RowCancelingEdit" DataKeyNames="Id" CellPadding="4"  
  3. OnRowEditing="gvImage_RowEditing" OnRowUpdating="gvImage_RowUpdating" OnRowDeleting="gvImage_RowDeleting" HeaderStyle-BackColor="Black" HeaderStyle-ForeColor="White" >  
  4. <Columns>  
  5. <asp:TemplateField HeaderText="Id" HeaderStyle-Width="70px" >  
  6. <ItemTemplate>  
  7. <asp:Label ID="lblId" runat="server" Text='<%#Container.DataItemIndex+1%>'></asp:Label>  
  8. </ItemTemplate>  
  9. </asp:TemplateField>  
  10. <asp:TemplateField HeaderText="Business Name" HeaderStyle-Width="200px">  
  11. <ItemTemplate>  
  12. <asp:Label ID="lblBname" runat="server" Text='<%# Eval("Bussiness_name") %>'></asp:Label>  
  13. </ItemTemplate>  
  14. <EditItemTemplate>  
  15. <asp:TextBox ID="txt_BName" runat="server" Text='<%# Eval("Bussiness_name") %>'></asp:TextBox>  
  16. </EditItemTemplate>  
  17. </asp:TemplateField>  
  18. <asp:TemplateField HeaderText="City" HeaderStyle-Width="200px">  
  19. <ItemTemplate>  
  20. <asp:Label ID="lblcity" runat="server" Text='<%# Eval("city") %>'></asp:Label>  
  21. </ItemTemplate>  
  22. <EditItemTemplate>  
  23. <asp:TextBox ID="txt_city" runat="server" Text='<%# Eval("city") %>'></asp:TextBox>  
  24. </EditItemTemplate>  
  25. </asp:TemplateField>  
  26. <asp:TemplateField HeaderText="Name" HeaderStyle-Width="200px">  
  27. <ItemTemplate>  
  28. <asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>  
  29. </ItemTemplate>  
  30. <EditItemTemplate>  
  31. <asp:TextBox ID="txt_name" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>  
  32. </EditItemTemplate>  
  33. </asp:TemplateField>  
  34. <asp:TemplateField HeaderText="Toll Free No." HeaderStyle-Width="200px">  
  35. <ItemTemplate>  
  36. <asp:Label ID="lbltoll" runat="server" Text='<%# Eval("toll_free") %>'></asp:Label>  
  37. </ItemTemplate>  
  38. <EditItemTemplate>  
  39. <asp:TextBox ID="txt_toll" runat="server" Text='<%# Eval("toll_free") %>'></asp:TextBox>  
  40. </EditItemTemplate>  
  41. </asp:TemplateField>  
  42. <asp:TemplateField HeaderText="Website Link" HeaderStyle-Width="200px">  
  43. <ItemTemplate>  
  44. <asp:Label ID="lblweb" runat="server" Text='<%# Eval("website_link") %>'></asp:Label>  
  45. </ItemTemplate>  
  46. <EditItemTemplate>  
  47. <asp:TextBox ID="txt_link" runat="server" Text='<%# Eval("website_link") %>'></asp:TextBox>  
  48. </EditItemTemplate>  
  49. </asp:TemplateField>  
  50. <asp:TemplateField HeaderText="Payment Mode" HeaderStyle-Width="200px">  
  51. <ItemTemplate>  
  52. <asp:Label ID="lblpay" runat="server" Text='<%# Eval("payment_mode") %>'></asp:Label>  
  53. </ItemTemplate>  
  54. <EditItemTemplate>  
  55. <asp:TextBox ID="txt_pay" runat="server" Text='<%# Eval("payment_mode") %>'></asp:TextBox>  
  56. </EditItemTemplate>  
  57. </asp:TemplateField>  
  58. <asp:TemplateField HeaderText="Email" HeaderStyle-Width="200px">  
  59. <ItemTemplate>  
  60. <asp:Label ID="lblmail" runat="server" Text='<%# Eval("email") %>'></asp:Label>  
  61. </ItemTemplate>  
  62. <EditItemTemplate>  
  63. <asp:TextBox ID="txt_email" runat="server" Text='<%# Eval("email") %>'></asp:TextBox>  
  64. </EditItemTemplate>  
  65. </asp:TemplateField>  
  66. <asp:TemplateField HeaderText="Image" HeaderStyle-Width="200px">  
  67. <ItemTemplate>  
  68. <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Images") %>'  
  69. Height="80px" Width="100px" />  
  70. </ItemTemplate>  
  71. <EditItemTemplate>  
  72. <asp:Image ID="img_user" runat="server" ImageUrl='<%# Eval("Images") %>'  
  73. Height="80px" Width="100px" /><br />  
  74. <asp:FileUpload ID="FileUpload1" runat="server" />  
  75. </EditItemTemplate>  
  76. </asp:TemplateField>  
  77. <asp:TemplateField HeaderStyle-Width="150px">  
  78. <ItemTemplate>  
  79. <asp:LinkButton ID="lbl_edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>  
  80. <asp:LinkButton ID="lbl_delete" runat="server" CommandName="Delete">Delete</asp:LinkButton>  
  81. </ItemTemplate>  
  82. <EditItemTemplate>  
  83. <asp:LinkButton ID="lbl_update" runat="server" CommandName="Update">Update</asp:LinkButton>  
  84. <asp:LinkButton ID="lbl_cancle" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>  
  85. </EditItemTemplate>  
  86. </asp:TemplateField>  
  87. </Columns>  
  88. </asp:GridView>  
code behind:
  1. namespace Jobopedia  
  2. {  
  3.     public partial class SP_edit : System.Web.UI.Page  
  4.     {  
  5.         SqlConnection con;  
  6.         SqlDataAdapter da;  
  7.         DataSet ds;  
  8.         SqlCommand cmd;  
  9.         string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;  
  10.         string path = "~/uploads/";  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             if (!this.IsPostBack)  
  14.             {  
  15.                 //this.BindGrid();  
  16.                 ImageData();  
  17.             }  
  18.         }  
  19.         protected void ImageData()  
  20.         {  
  21.             con = new SqlConnection(constr);  
  22.             con.Open();  
  23.             da = new SqlDataAdapter("select * from Q_SP_Afterlogin", con);  
  24.             ds = new DataSet();  
  25.             da.Fill(ds);  
  26.             gvImage.DataSource = ds;  
  27.             gvImage.DataBind();  
  28.         }  
  29.   
  30.         protected void gvImage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)  
  31.         {  
  32.             gvImage.EditIndex = -1;  
  33.             ImageData();  
  34.         }  
  35.   
  36.         protected void gvImage_RowEditing(object sender, GridViewEditEventArgs e)  
  37.         {  
  38.             gvImage.EditIndex = e.NewEditIndex;  
  39.             ImageData();  
  40.         }  
  41.   
  42.         protected void gvImage_RowUpdating(object sender, GridViewUpdateEventArgs e)  
  43.         {  
  44.             //find image id of edit row      
  45.             string Id = gvImage.DataKeys[e.RowIndex].Value.ToString();  
  46.             // find values for update      
  47.             TextBox txt_BName = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_BName");  
  48.             TextBox txt_city = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_city");  
  49.             TextBox txt_name = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_name");  
  50.             TextBox txt_toll = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_toll");  
  51.             TextBox txt_link = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_link");  
  52.             TextBox txt_pay = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_pay");  
  53.             TextBox txt_email = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_email");  
  54.             FileUpload FileUpload1 = (FileUpload)gvImage.Rows[e.RowIndex].FindControl("FileUpload1");  
  55.             con = new SqlConnection(constr);  
  56.               
  57.             string path = "~/uploads/";  
  58.             if (FileUpload1.HasFile)  
  59.             {  
  60.                 path += FileUpload1.FileName;  
  61.                 //save image in folder      
  62.                 FileUpload1.SaveAs(MapPath(path));  
  63.             }  
  64.             else  
  65.             {  
  66.                 // use previous user image if new image is not changed      
  67.                 Image img = (Image)gvImage.Rows[e.RowIndex].FindControl("img_user");  
  68.                 path = img.ImageUrl;  
  69.             }  
  70.             SqlCommand cmd = new SqlCommand("update Q_SP_Afterlogin set Bussiness_name='" + txt_BName.Text + "',city='" + txt_city.Text + "',name='" + txt_name.Text + "',toll_free='" + txt_toll.Text + "',website_link='" + txt_link.Text + "',payment_mode='" + txt_pay.Text + "',email='" + txt_email.Text + "',Images='" + path + "'  where Id=" + Id + "", con);  
  71.             con.Open();  
  72.             cmd.ExecuteNonQuery();  
  73.             con.Close();  
  74.             gvImage.EditIndex = -1;  
  75.             ImageData();  
  76.         }  
  77.   
  78.         protected void gvImage_RowDeleting(object sender, GridViewDeleteEventArgs e)  
  79.         {  
  80.             GridViewRow row = (GridViewRow)gvImage.Rows[e.RowIndex];  
  81.             Label Id = (Label)row.FindControl("lblId");  
  82.             Label Bussiness_name = (Label)row.FindControl("lblBname");  
  83.             Label city = (Label)row.FindControl("lblcity");  
  84.             Label name = (Label)row.FindControl("lblname");  
  85.             Label toll_free = (Label)row.FindControl("lbltoll");  
  86.             Label website_link = (Label)row.FindControl("lblweb");  
  87.             Label payment_mode = (Label)row.FindControl("lblpay");  
  88.             Label email = (Label)row.FindControl("lblmail");  
  89.             //Image Images = (Image)row.FindControl("Image1");  
  90.             con = new SqlConnection(constr);  
  91.             con.Open();  
  92.             SqlCommand cmd = new SqlCommand("delete FROM Q_SP_Afterlogin where Id='" + Convert.ToInt32(gvImage.DataKeys[e.RowIndex].Value.ToString()) + "'", con);  
  93.             cmd.ExecuteNonQuery();  
  94.             con.Close();  
  95.             ImageDeleteFromFolder(path);    // path is folder path    
  96.             ImageData();  
  97.         }  
  98.         protected void ImageDeleteFromFolder(string Images)  /* In Images path is being stored  */  
  99.         {  
  100.             string file_name = Images;  
  101.             string path = Server.MapPath("~/uploads/");  
  102.             string filePath = path + file_name;  
  103.             FileInfo file = new FileInfo(filePath);  
  104.             if (file.Exists) //check file exsit or not  
  105.             {  
  106.                 file.Delete();  
  107.                 lblResult.Text = " file deleted successfully";  
  108.                 lblResult.ForeColor = System.Drawing.Color.Green;  
  109.             }  
  110.             else  
  111.             {  
  112.                 lblResult.Text = " This file does not exists ";  
  113.                 lblResult.ForeColor = System.Drawing.Color.Red;  
  114.             }  
  115.   
  116.         }  
  117.   
  118.          
  119.     }  
  120. }  
 

Attachment: code.rar

Answers (11)