Delete data from Database and Image from Folder (Directory) using C# and VB.Net in ASP.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

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 being stored.

code behind

namespace Jobopedia
{
    public partial class SP_edit : System.Web.UI.Page
    {
        SqlConnection con;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommand cmd;
        string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //this.BindGrid();
                ImageData();
            }
        }
        protected void ImageData()
        {
            con = new SqlConnection(constr);
            con.Open();
            da = new SqlDataAdapter("select * from Q_SP_Afterlogin", con);
            ds = new DataSet();
            da.Fill(ds);
            gvImage.DataSource = ds;
            gvImage.DataBind();
        }        

        protected void gvImage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            GridViewRow row = (GridViewRow)gvImage.Rows[e.RowIndex];
            Label Id = (Label)row.FindControl("lblId");
            Label Bussiness_name = (Label)row.FindControl("lblBname");
            Label city = (Label)row.FindControl("lblcity");
            Label name = (Label)row.FindControl("lblname");
            Label toll_free = (Label)row.FindControl("lbltoll");
            Label website_link = (Label)row.FindControl("lblweb");
            Label payment_mode = (Label)row.FindControl("lblpay");
            Label email = (Label)row.FindControl("lblmail");
            //Label Images = (Label)row.FindControl("Image1");
            con = new SqlConnection(constr);
            con.Open();
            SqlCommand cmd = new SqlCommand("delete FROM Q_SP_Afterlogin where Id='" + Convert.ToInt32(gvImage.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
            cmd.ExecuteNonQuery();
            con.Close();
            ImageDeleteFromFolder(name.Text);
            ImageData();
        }
        protected void ImageDeleteFromFolder(string Images)
        {
            string file_name = Images;
            string path = Server.MapPath("~/uploads/");
            FileInfo file = new FileInfo(path);
            if (file.Exists) //check file exsit or not  
            {
                file.Delete();
                lblResult.Text =" file deleted successfully";
                lblResult.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lblResult.Text =" This file does not exists ";
                lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }       
    }
}

 

This question does not have replies that have been liked.