Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am able to delete from database but i do not know how delete image from folder.
Please help .
for better understanding please edit my code and paste.
Thanks in advance.

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand command = new SqlCommand("SPinsertitem", con);
        command.CommandType = CommandType.StoredProcedure;
// UPLOAD IMAGE 1 //
        string strname = picture1.FileName.ToString();
        strname = "~//brand//New folder//" + strname;
        picture1.PostedFile.SaveAs(Server.MapPath(strname));
        command.Parameters.Add("@imageone", SqlDbType.VarChar, 100).Value = strname;
        // UPLOAD IMAGE 2 //
        string strname2 = picture2.FileName.ToString();
        strname2 = "~//brand//New folder//" + strname2;
        picture2.PostedFile.SaveAs(Server.MapPath(strname2));
        command.Parameters.Add("@imagetwo", SqlDbType.VarChar, 100).Value = strname2;
        // UPLOAD IMAGE 3 // 
        string strname3 = picture3.FileName.ToString();
        strname3 = "~//brand//New folder//" + strname3;
        picture3.PostedFile.SaveAs(Server.MapPath(strname3));
        command.Parameters.Add("@imagethree", SqlDbType.VarChar, 100).Value = strname3;

protected void BindGrid()
    {

        SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM pikiphones", con);
        DataSet ds = new DataSet();
        myCommand.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

protected void delete_rOW(object sender, GridViewDeleteEventArgs e)
    {
        String deleteCmd = "DELETE FROM pikiphones WHERE id= @id";
        SqlCommand myCommand = new SqlCommand(deleteCmd, con);
        myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
        myCommand.Parameters["@id"].Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
        try
        {
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException)
        {
            Label1.Text = "ERROR: Could not delete record";

        }
        myCommand.Connection.Close();
        
        BindGrid();
    }
Posted
Updated 3-Jan-17 6:09am

I see nowhere where you try to delete the file.

You can just use Systme.IO.File.Delete(path);, but there is no need to save the file to the file system anyway:

set up the image field as a varbinary(max):

SQL
Create Table images(
  image_id int not null identity(1,1) primary key,
  image_image1_name nvarchar(max),
  image_image1_binary varbinary(max)
-- ...
)


Then read the file stream directly from the upload. No need to save it, just keep it in memory as a byte[] for a couple of cycles.

C#
//....
    byte[] input = new byte[picture1.ContentLength];

    var stream = picture1.InputStream;

    stream.Read(input, 0, picture1.ContentLength);
    
    command.Parameters.Add("@image1Name", SqlDbType.VarChar, 100).Value=picture1.FileName;
    command.Parameters.Add("@image1Byte", SqlDbType.VarBinary, file.Length).Value=input;



This is how I upload files all over the place. It's much faster and less hastle to clean up as the GC will bin the file when you're finished with it.

I hope that helps ^_^
 
Share this answer
 
Comments
navi G 3-Jan-17 7:57am    
hi, Andy Lang please tell where i have to write Systme.IO.File.Delete(path);. for example please edit my code then pase.
Andy Lanng 3-Jan-17 8:32am    
replace (path) for (strname) then put that line where you want to delete the file.

I suggest you do it after writing it to the database. You haven't posted that section of code so I cannot rewrite it for you
navi G 3-Jan-17 11:52am    
protected void delete_rOW(object sender, GridViewDeleteEventArgs e)
{
String deleteCmd = "DELETE FROM pikiphones WHERE id= @id";
SqlCommand myCommand = new SqlCommand(deleteCmd, con);
myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
myCommand.Parameters["@id"].Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
try
{
myCommand.ExecuteNonQuery();
}
catch (SqlException)
{
Label1.Text = "ERROR: Could not delete record";

}
myCommand.Connection.Close();

BindGrid();
}
navi G 3-Jan-17 11:55am    
thsi code i am using for delete . with this i am able to delete from database but i dont know how to delete from folder with this code as your suggested code System.IO.File.Delete(path).
navi G 3-Jan-17 11:57am    
i don't know how i describe my problem but please help
Addendum to last solution:

Ok - first you need to try to delete the item, preferably before the reference, but it doesn't matter. Just make sure you get the path before deleting the entry:

C#
protected void delete_rOW(object sender, GridViewDeleteEventArgs e)
    {


        //I'll write this "best practice"

        var id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text) 
        string selectCmd = "select path from pikiphones WHERE id= @id"
        using(SqlCommand com = con.CreateCommand){
            com.Text = selectCmd;
            com.Parameters.Add(new SqlParameter("@id", SqlDbType.Int){Value=id});
            var path = com.ExecuteScalar().toString();

            if(!string.isNullOrEmpty(path) && Path.Exists(path)){
               //try catch?
               Path.Delete(path);
            }
        }


        String deleteCmd = "DELETE FROM pikiphones WHERE id= @id";
        SqlCommand myCommand = new SqlCommand(deleteCmd, con);
        myCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
        myCommand.Parameters["@id"].Value = id;
        try
        {
            myCommand.ExecuteNonQuery();
        }
        catch (SqlException)
        {
            Label1.Text = "ERROR: Could not delete record";

        }
        myCommand.Connection.Close();
        
        BindGrid();
    }



You may have to debug it. I know I have some method keywords wrong. Let me know if you get stuck ^_^
 
Share this answer
 

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