0
Answer

how to replace a existing picture without adding another one

Israel

Israel

10y
790
1

Hi!


I have "fileuploadimages" and a gridview tools. When I ipload a picture it's doing well the job. But it's adding a picture after another on in my gridview.

Then what I want to do it's ONLY to replace the latest picture to another one that I upload (e: existing/replacing and so one).

This my codes:



<asp:FileUpload ID="fileuploadimages" runat="server" />

    <asp:Button ID="btnsave" runat="server" Text="Upload" onclick="btnSubmit_Click" />

    </div>

    <asp:GridView runat="server" ID="gvImages" AutoGenerateColumns="false"

            DataSourceID="sqldataImages" CssClass="Gridview"

            HeaderStyle-BackColor="#61A6F8">

    <Columns>

    <asp:ImageField HeaderText="Image" DataImageUrlField="ImagePath" ShowHeader="False" />

   

    </Columns>

    </asp:GridView>


protected void btnSubmit_Click(object sender, EventArgs e)

    {

        //Get Filename from fileupload control

        string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);

        //Save images into Images folder

        fileuploadimages.SaveAs(Server.MapPath("~/Images/" + filename));

        //Getting dbconnection from web.config connectionstring

        //SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["master"].ToString());

        //Open the database connection

        con.Open();

        //Query to insert images path and name into database

        SqlCommand cmd = new SqlCommand("Insert into ImagesPath(First_Name,ImageName,ImagePath) values(@First_Name,@ImageName,@ImagePath)", con);

        //Passing parameters to query

        //cmd.Parameters.AddWithValue("@First_Name", fname.Text);

        cmd.Parameters.AddWithValue("@ImageName", filename);

        cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + filename);

        cmd.ExecuteNonQuery();

        //Close dbconnection

        con.Close();

        Response.Redirect("~/English/Visual.aspx");

    }