Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want the images to be deleted from server as well as from database when user clicks the delete button.

My Code is:-

C#
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string str = @"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true";
        SqlConnection con = new SqlConnection(str);
        con.Open();
        string s = GridView1.DataKeys[e.RowIndex].Value.ToString();

        SqlCommand cmd = new SqlCommand("Delete from date1 where ddate='" + s + "'",con);
        cmd.ExecuteNonQuery();
        con.Close();


        Response.Write("Deleted Sucessfully");
    }
Posted
Updated 28-Dec-14 5:46am
v2
Comments
DamithSL 28-Dec-14 11:50am    
what is your question? your delete not happen with current code?
how you bind data to gridview? what is the select statement for that?
how you set gridview datakeys?
what is the column data type of ddate column in your database?
any exceptions in above code?
update the question with above details.
Deepak Kanswal Sharma 29-Dec-14 11:06am    
My delete is happening. But only pic's detail in database. The pic is not getting deleted from server.
For binding I use sqlserver command.
Select Command:-Select distinct ddate from date1
ddate column data type:- date
No exception in code.

If you store the actual image data in your database, in the date1 table, then that code will delete the image data. If you store it in a different table, then you will need to enable cascade deletes[^] or write teh code to manually remove them from the other table as well.

If you store it in a disk file and store a path to the file in your DB, then you are best off deleting that from your C# code using File.Delete[^] as your webserver will almost certainly have access to the folder holding the image data, and the SQL server computer probably won't.
 
Share this answer
 
Hi,
For deleting image from database you can write a delete query
And for deleting image from server you can use folling code

C#
string file_name = GridView1.DataKeys[e.RowIndex].Value.ToString(); //your Image name
  string path = Server.MapPath("YourImageFolderName//" + file_name);
  FileInfo file = new FileInfo(path);
  if (file.Exists)//check file exsit or not
  {
       file.Delete();
  }
  else
  {
      //Show msg "This file does not exists "
  }


Thanks
 
Share this answer
 
Comments
Deepak Kanswal Sharma 3-Jan-15 9:20am    
Thanks bro, but can you explain the line:-

FileInfo file = new FileInfo(path);

Do I have to use some another library files for this?
My c# shows it is not a correct format and cannot search the 'FileInfo'.
Suvendu Shekhar Giri 4-Jan-15 15:57pm    
Make sure that you have included System.IO namespace?
Deepak Kanswal Sharma 4-Jan-15 21:02pm    
Ok I included that namespace System.IO

and applied the following code:-
string file_name = GridView2.DataKeys[e.RowIndex].Value.ToString(); //your Image name
string path = Server.MapPath("Img//" + file_name);
FileInfo file = new FileInfo(path);
if (file.Exists)//check file exsit or not
{
file.Delete();
}
else
{
//"File does not exist";
}

Still The image file is not deleted from server.
My folder name is 'Img';

Please HELP!! Thanks!!
Sumit Jawale 4-Jan-15 23:34pm    
Hi Deepak,
Try to debug your code. Please see what path it is stored in path variable (For line string path = Server.MapPath("Img//" + file_name);)
And please try to hit that path, whether it is giving you a right path or not..
Please let me know your result of debugging.
Deepak Kanswal Sharma 6-Jan-15 10:04am    
The string "path" is showing null value. :(
I tried both double slash(//) as well as single slash (/). But no use!!

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