Edit and Update Image using ASP.Net GridView RowUpdating in C#

Last Reply one month ago By itsme

Posted one month ago

How to update image name in database

    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 where email='" + Session["mail"].ToString() + "'", con);
            ds = new DataSet();
            da.Fill(ds);
            gvImage.DataSource = ds;
            gvImage.DataBind();
        }

        protected void gvImage_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvImage.EditIndex = -1;
            ImageData();
        }

        protected void gvImage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvImage.EditIndex = e.NewEditIndex;
            ImageData();
        }

        protected void gvImage_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //find image id of edit row    
            string Id = gvImage.DataKeys[e.RowIndex].Value.ToString();
            // find values for update    
            TextBox BName = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_BName");
            TextBox city = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_city");
            TextBox name = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_name");
            TextBox add = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_add");
            TextBox toll = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_toll");
            TextBox link = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_link");
            TextBox pay = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_pay");
            TextBox email = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_email");
            TextBox images = (TextBox)gvImage.Rows[e.RowIndex].FindControl("txt_img");
            FileUpload FileUpload1 = (FileUpload)gvImage.Rows[e.RowIndex].FindControl("FileUpload1");
            con = new SqlConnection(constr);
            
            string path = "~/uploads/";
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            if (FileUpload1.HasFile)
            {
                path += FileUpload1.FileName;
                //save image in folder    
                FileUpload1.SaveAs(MapPath(path));
            }
            else
            {
                // use previous user image if new image is not changed    
                Image img = (Image)gvImage.Rows[e.RowIndex].FindControl("img_user");
                path = img.ImageUrl;
            }
            SqlCommand cmd = new SqlCommand("update Q_SP_Afterlogin set Bussiness_name='" + BName.Text + "',address='" + add.Text + "',Image_name='" +images.Text+ "',city='" + city.Text + "',name='" + name.Text + "',toll_free='" + toll.Text + "',website_link='" + link.Text + "',payment_mode='" + pay.Text + "',email='" + email.Text + "',Images='" + path + "'  where Id=" + Id + "", con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            gvImage.EditIndex = -1;
            ImageData();
        }       
    }

 

This question does not have replies that have been liked.