Retain (Keep) previous uploaded FileName if TextBox vale is Empty using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

Hey

I am trying to edit my records which is functioning very well but when I dont upload any image on editing and click on update button then image name is set as null because nothing is being uploaded.

But it old image name should be as it is if nothing is being uploaded

thanks

    public partial class SP_edit_new : System.Web.UI.Page
    {
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow grow = btn.NamingContainer as GridViewRow;
            string path = "~/uploads/";
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            if (FileUpload1.HasFile)
            {
                path += FileUpload1.FileName;
                FileUpload1.SaveAs(MapPath(path));
                UpdateFile(path);
                ClearControls();
            }
            else
            {
                path = Image1.ImageUrl;
               
                UpdateFile(path);
                FillGrid();
            }
            FillGrid();
            lblMessage.Text = "Updated Successfully.";
        }

        private void UpdateFile(string path)
        {

            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            List<String> open = new List<String>();
            if (ddlTimeFrom.SelectedIndex > 0 && ddlTimeTo.SelectedIndex > 0)
            {
                open.Add(ddlTimeFrom.Text);
                open.Add(ddlTimeTo.Text);
            }
            string open2 = string.Join(", ", open);
            string constr = ConfigurationManager.ConnectionStrings["Q_SS_regnConnectionString1"].ConnectionString;
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("UPDATE SP_Afterlogin2 SET Images =@Path,Bussiness_name=@Bussiness_name,city=@city,Image_name=@Image_name,name=@name,toll_free=@toll_free,website_link=@website_link,payment_mode=@payment_mode,email=@email,address=@address,h_open=@h_open WHERE Id =@Id", con);
            cmd.Parameters.AddWithValue("@Id", Id.Value);
            cmd.Parameters.AddWithValue("@Path", path);
            cmd.Parameters.AddWithValue("@Bussiness_name", DropDownList1.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@city", DropDownList2.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@name", txt_name.Text);
            cmd.Parameters.AddWithValue("@toll_free", txt_toll.Text);
            cmd.Parameters.AddWithValue("@website_link", txt_link.Text);
            cmd.Parameters.AddWithValue("@payment_mode", txt_pay.Text);
            cmd.Parameters.AddWithValue("@email", txt_email.Text);
            cmd.Parameters.AddWithValue("@Image_name",fileName);
            cmd.Parameters.AddWithValue("@address", txt_Address.Text);
            cmd.Parameters.AddWithValue("@h_open", open2);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }

 

This question does not have replies that have been liked.