Aktham Mahmoud

Aktham Mahmoud

  • NA
  • 720
  • 34.9k

Image Deleted from folder But not update table

Jul 5 2020 5:33 AM
Greeting all
I viewed  these article by Upendra Pratap Shahi to make (CRUD) on Image by Gridview 
 
https://www.c-sharpcorner.com/UploadFile/77a82c/binding-image-with-gridview-and-update-image-in-gridview/ 
 
Actually I use a piece from last article to make delete photo from folder (Not delele a row completly) see fig.1
 
 
A methode to delete photo from folder by Upendra Pratap Shahi :
 
  1. protected void ImgDelFrmfolder(string imagename)  
  2.       {  
  3.   
  4.           string file_name = imagename;  
  5.           string path = Server.MapPath(@"~/images/products/" + imagename);  
  6.           FileInfo file = new FileInfo(path);  
  7.           if (file.Exists)//check file exsit or not  
  8.           {  
  9.                file.Delete();  
  10.                LBgvSu.Text = file_name + " file deleted successfully";  
  11.           }  
  12.           else  
  13.           {  
  14.               LBgvFi.Text = file_name + " This file does not exists ";  
  15.   
  16.           }  
  17.   
  18.       }  
 
I add Button under Image to Delete Image:
  1. <ItemTemplate>    
  2. <div style="text-align:center">    
  3. <asp:Image ID="IMG_GVProduct" runat="server" Height="80px" ImageUrl='<%# Eval("P_photo") %>' Width="80px" /><br />    
  4. <asp:Button ID="DelImg" runat="server" Height="25" Text="Delete Photo" CssClass="btn btn-outline-danger" Font-Size="Small" Font-Overline="False"  CommandName="Del_photo" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'/>    
  5. </div>   
  6. </ItemTemplate> 
What I add extara Idea and extara code?
My Idea is to Update a two colums  in table (photo_path and Photo_name) with (Null) value when an admin delete image.
My update code, is calling command name ("Del_photo") for Delete button, by ("GridView1_RowCommand"), see code below:
  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)  
  2. {  
  3.   
  4.     if (e.CommandName == "Del_photo")  
  5.     {  
  6.   
  7.         //Determine the RowIndex of the Row whose Button was clicked.  
  8.         int rowInx = Convert.ToInt32(e.CommandArgument);  
  9.   
  10.         //Reference the GridView Row.  
  11.         GridViewRow row = GridView1.Rows[rowInx];  
  12.   
  13.         //Fetch value of Name.  
  14.         string Lb_name = (row.FindControl("Lb_ImgName"as Label).Text;  
  15.   
  16.         //Call Deleted Image from folder Methode  
  17.         ImgDelFrmfolder(Lb_name);  
  18.   
  19.         using (SqlConnection sqlcon = new SqlConnection(connectionString))  
  20.             try  
  21.             {  
  22.                 sqlcon.Open();  
  23.                 string query = "UPDATE Products SET P_photo = @P_photo, Pho_Name=@Pho_Name WHERE (Id = @Id)";  
  24.                 SqlCommand sqlcmd2 = new SqlCommand(query, sqlcon);  
  25.                 sqlcmd2.Parameters.AddWithValue("@P_photo""");  
  26.                 sqlcmd2.Parameters.AddWithValue("@Pho_Name""");  
  27.                 sqlcmd2.Parameters.AddWithValue("@Id", Convert.ToInt32(rowInx.ToString()));  
  28.                 sqlcmd2.ExecuteNonQuery();  
  29.                 GridView1.EditIndex = -1;  
  30.                 sqlcon.Close();  
  31.                 GridView1.DataBind();  
  32.             }  
  33.             catch (Exception ex)  
  34.             {  
  35.                 LBgvFi.Text = ex.Message;  
  36.             }  
  37.     }  
  38.   
  39. }  
 What happen?
a code run correctly, no bugs no errors, and an image delete from folder complety.
See fig.2 below:
 
 But if I back to table and check a new changes for two columns nothing happen(to clear old values and keep it null).
 
 
I don't know what a true method if I have wrong way, or my coding need to rebuilding; 
I'm trying and still until this moment to fix that.
Thnks for any help 

Answers (2)