locked
delete image from folder RRS feed

  • Question

  • User-807418713 posted

    Hello

    I use this Server.MapPath("~/ProductImage/" To upload image

    Now i want to show this all image in gridview with fileuploaded date

    If i click delete it should delete the image from folder..

    How to do so using asp.net 2.0 C#

    Thank You

    Saturday, December 8, 2018 9:28 AM

Answers

  • User-893317190 posted

    Hi Gopi.MCA,

    If you want to use image control to show the image and delete the file in your server,you could try the code below.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"  AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                    <asp:TemplateField HeaderText="images">
                        <ItemTemplate>
                                           imagepathe is the file name, /images/WingtipToysBak is the folder pateh where the image is in
                            <asp:Image ID="Image1" runat="server" ImageUrl='<%# "/images/WingtipToysBak/"+Eval("ImagePath") %>' /> 
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
                </Columns>
            </asp:GridView>

    Code behind.

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridview();
                }
            }
            private void BindGridview()
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("select * from productBak ", constr))
                {
    
    
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
            private static string constr = ConfigurationManager.ConnectionStrings["WingtipToysConnectionString"].ConnectionString;
            protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("delete from [productBak] where productid =@id ", con))
                    {
    
                        com.Parameters.AddWithValue("id", GridView1.DataKeys[e.RowIndex].Value);
                        Image image = GridView1.Rows[e.RowIndex].FindControl("Image1") as Image;
                        File.Delete(Server.MapPath(image.ImageUrl));  //get the absolute of image path and delete from the server
                        con.Open();
                        com.ExecuteNonQuery();
                    }
                }
                BindGridview();
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 10, 2018 6:34 AM

All replies

  • User-1716253493 posted

    Store filename only to database

    Use ~/ProdectImage/{0} format for navigateUrl

    When deleting, get the filename from database based deleted id, or from the grid row value

    Use same format to get the server path to delete

    File.Delete(Server.MapPath(String.Format("~/ProdectImage/{0}",filename)))

    When 

    Sunday, December 9, 2018 2:08 AM
  • User-807418713 posted

    Hello

    CAN YOU GIVE ME ONE SAMPLE CODE ITS EASY TO UNDERSTAND HOW IT WORKS

    Sunday, December 9, 2018 7:23 AM
  • User-1716253493 posted

    This sample getting filename from hyperlinkfield, you can also get it from database base id value

        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" DataKeyNames="ID" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="filename" DataNavigateUrlFormatString="~/ProductImage/{0}" DataTextField="filename" />
            </Columns>
        </asp:GridView>
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            HyperLink h = (HyperLink)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
            string filepath = Server.MapPath(string.Format("~/ProductImage/{0}", h.Text));
            System.IO.File.Delete(filepath);
        }

    Sunday, December 9, 2018 7:45 AM
  • User-1822989843 posted

    Dear Gopi,

    The following Link will help you with your Issue:

    https://www.c-sharpcorner.com/UploadFile/77a82c/binding-image-with-gridview-and-update-image-in-gridview/

    Please provide permission/access to IIS Users to the folder where you want to keep the Uploaded files.

    On a personal note, try upgrading to .Net 4.5 with MVC & EF.

    Regards,

    Vishal

    Sunday, December 9, 2018 7:14 PM
  • User-893317190 posted

    Hi Gopi.MCA,

    If you want to use image control to show the image and delete the file in your server,you could try the code below.

     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"  AutoGenerateDeleteButton="true" OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                    <asp:TemplateField HeaderText="images">
                        <ItemTemplate>
                                           imagepathe is the file name, /images/WingtipToysBak is the folder pateh where the image is in
                            <asp:Image ID="Image1" runat="server" ImageUrl='<%# "/images/WingtipToysBak/"+Eval("ImagePath") %>' /> 
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
                </Columns>
            </asp:GridView>

    Code behind.

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindGridview();
                }
            }
            private void BindGridview()
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("select * from productBak ", constr))
                {
    
    
                    DataTable table = new DataTable();
                    adapter.Fill(table);
                    GridView1.DataSource = table;
                    GridView1.DataBind();
                }
            }
            private static string constr = ConfigurationManager.ConnectionStrings["WingtipToysConnectionString"].ConnectionString;
            protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand com = new SqlCommand("delete from [productBak] where productid =@id ", con))
                    {
    
                        com.Parameters.AddWithValue("id", GridView1.DataKeys[e.RowIndex].Value);
                        Image image = GridView1.Rows[e.RowIndex].FindControl("Image1") as Image;
                        File.Delete(Server.MapPath(image.ImageUrl));  //get the absolute of image path and delete from the server
                        con.Open();
                        com.ExecuteNonQuery();
                    }
                }
                BindGridview();
            }

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 10, 2018 6:34 AM
  • User-1716253493 posted

    Dear Gopi,

    The following Link will help you with your Issue:

    https://www.c-sharpcorner.com/UploadFile/77a82c/binding-image-with-gridview-and-update-image-in-gridview/

    Please provide permission/access to IIS Users to the folder where you want to keep the Uploaded files.

    On a personal note, try upgrading to .Net 4.5 with MVC & EF.

    Regards,

    Vishal

    Welcome back Vishal

    Monday, December 10, 2018 6:45 AM