Click here to Skip to main content
15,892,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all .

i have 2 buttons one of them upload an image and another delete it .
In my code there is an <asp:image> also which as soon as upload image asp:image show that image and after deleting image should disappear from the form .

my add button has the below code :

C#
protected void Btn_upload_Click(object sender, EventArgs e)
{

    //To upluad image into tbl_Img and get its id .
    try
    {
        con.Open();
        SqlCommand cmdInsert = new SqlCommand("insert into tbl_Img (Fld_Uploader,Fld_path,Fld_FileName) values ('Admin','" + PublicClass.ImgPath() + "','" + File_Upload.FileName + "')", con);
        cmdInsert.ExecuteNonQuery();

        SqlCommand cmdGetId = new SqlCommand("select top 1 * from tbl_img order by fld_id desc", con);
        SqlDataAdapter da = new SqlDataAdapter(cmdGetId);
        DataTable dt = new DataTable();
        da.Fill(dt);

        ImgID = dt.Rows[0]["Fld_id"].ToString();
        RandCode = dt.Rows[0]["Fld_RandCode"].ToString();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    try
    {
        ImgPath = Server.MapPath(PublicClass.ImgPath() + ImgID + "_" + File_Upload.FileName);
        File_Upload.SaveAs(ImgPath);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

    Img.ImageUrl = "../../ReadAttachment.aspx?code=" + RandCode + "&maxWidth=100&maxHeight=100"; //the problem is from here . if i comment this line there will be no error . 

    BtnDelete.Visible = true;
    btnAdd.Enabled = true;
} 


and my Delete btn has this code :

C#
protected void BtnDelete_Click(object sender, EventArgs e)
{
    try
    {
        if (System.IO.File.Exists(ImgPath))
        {
            Img.ImageUrl = "";
            System.IO.File.Delete(ImgPath); //ImgPath is a global varoable
            Response.Write("Deleted");
        }
        else
        {
            Response.Write("its not exist");
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
}


but at delete button i get this error :

The process cannot access the file 'L:\chikardarin Website\WebApplication2\image_logo\11_nissan-cars-logo-emblem.jpg' because it is being used by another process.


note : this example is run on localHost .
please tell me how to discard my image-url at delete time . thank you :)
Posted
Updated 18-Oct-14 2:57am
v4

1 solution

Check the permission of the folder where the images are uploaded on the web server.
 
Share this answer
 
Comments
Member 11125813 18-Oct-14 8:52am    
im testing on localhost now .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900