views:

375

answers:

2

I'm trying to delete a record from Gridview1 at the same time delete the corresponding image file off the server in a single click. (Each row in Gridview1 has an associated image file on the server.)

To delete the record, I'm using asp:CommandField showDeleteButton="true" along with a sqlDataSource's DELETE statement.

During that process, I'm also using GridView1's "onRowDeleting" event to delete the corresponding image file off the server.

Here's what it does, with the code I have below:
- The record indeed gets deleted
- The file on the server does not
- There are NO errors thrown (since I guess that it's not finding the file, and this is the expected behavior...

Also consider: I've already set up and tested a much simpler "see if files can actually be deleted off the server" test before I started development on the Gridview. Since our files are on a hosting company server, I wanted to test for any permissions issues. Whereby: Enter the fileName and extension into a text box ("myImage.jpg") Click the button that uses the File.Delete method WhaaLa - the file is gone from the server.

However, I can't get the file to go away with my new setup. Here's the code:

    <asp:GridView ID="GridView1" runat="server" AllowSorting="True"  AutoGenerateColumns="False" DataKeyNames="libraryID" 
                            DataSourceID="SqlDataSource1" Width="800px"  onrowdeleting="deleteImageFromServer" CssClass="gridViewSmallText" 
                              OnDataBound="rowCount">

                            <Columns>
                                <asp:CommandField ShowDeleteButton="True"  />

 <%--A link that goes to the uploadPage to upload a new version of the image--%>
              <asp:HyperLinkField runat="server" HeaderText="SKU (Click to Update)"  DataTextField="sku" DataNavigateUrlFields="sku" SortExpression="sku"  DataNavigateUrlFormatString="graphicUpload.aspx?sku={0}"  >
                   </asp:HyperLinkField>


                 <asp:TemplateField HeaderText="Image" SortExpression="imagePath">
                            <ItemTemplate>
<%--Pull the imagePath column from the database here-it also includes the image file --%>            
 <asp:Image ID="merchImage" runat="server" Height="100px" ImageUrl='<%# "http://www.ourcompanysite.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' /><br />
    </ItemTemplate>
      </asp:TemplateField>
          <asp:TemplateField HeaderText="View Full Size">
      <ItemTemplate>
 <%--A link to view the image in it's full size in a new browser window--%>
  <asp:HyperLink ID="fullSizeHyperlink" runat="server" NavigateUrl='<%# "http://www.leadingjewelersguild.net/" + DataBinder.Eval(Container.DataItem, "imagePath") %>' Text="View" Target="_blank"  />
      </ItemTemplate>

 </asp:TemplateField>
<asp:BoundField DataField="DateUpdated" </asp:BoundField>
<%---some date stuff here--%>
 <asp:BoundField DataField="DateCreated" HeaderText="First Uploaded"    SortExpression="DateCreated" >
     </asp:BoundField>
          </Columns>
      </asp:GridView>

Code behind:

    protected void deleteImageFromServer(object sender, GridViewDeleteEventArgs e)

    {

  // I read from GridViewGuy.com that you're supposed to reference the row item via e.Values

  string imageToDelete = e.Values["sku"] + ".jpg"; 

   //I pulled the value of "imageToDelete" into a lable just to see what I was getting   
//during the "onRowDeleting" and it reported back .jpg instead of the full file name 
//myImage.jpg, so I guess this is the crux of my problem.


   string image = Server.MapPath("/images/graphicsLib/" + imageToDelete);
   string image = Server.MapPath("e:\\sites\\oursite\\files\\images\\graphicsLib\\" + imageToDelete);

    if (File.Exists(image))
       {
            File.Delete(image);
       }


//at this point the record from GridView1 is gone, but the file on server remains.
}
A: 

After you started development using the GridView control, have you verified that the deleteImageFromServer method is being called by setting a breakpoint in there?

And that imageToDelete is being set correctly?

After breaking into it, look at the 'watch' values for 'sender' and 'e'. Drill down into those to see what kind of objects they are and what values they hold.

Sometimes you have to traverse the object hierarchy in the GridVied control to get to the right object.

M3NTA7
+1  A: 

I think part of your problem may be that in order for e.Values["sku"] to contain a value, it has to be bound first. I don't think HyperlinkField binds its data (I could be wrong on that though so don't quote me)

First try adding a <asp:BoundField DataField="sku" Visible="false" /> in your column list. or change the HyperLinkField to TemplateField and explicitly bind the sku '<%#Bind("sku")%>'

If that doesn't work you can try changing DataKeyNames="libraryID" to DataKeyNames="libraryID,sku". You should be able to get the value out of e.Keys["sku"], or e.Values["sku"].

Josh
Right on Josh, you got it: e.Keys when adding the "sku" value to the DataKeyNames - I never knew you could do that. Thanks,
Doug