Trying to delete a file that's in use

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vertigo262
    New Member
    • Jul 2007
    • 62

    #1

    Trying to delete a file that's in use

    I have some simple code that's basically loading data into a dataset and the databound handler does a resize on the image.

    This code also has a database delete query and a file delete.

    When the code executes for the file to be deleted, the image that's loaded on the web page throws an error

    "Access to the path 'D:\InetPub\Ree fJunkies\Users\ ganjaman\Corals \Picture\200808 27123704lobophy tum388wfsmall.j pg' is denied."

    I'm not using an image stream or anything like that. It seems like the dataset is locking the file.

    Here is the code

    Code:
    
    Aspx page
    
    
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
                <td style="background-image: url(/Images/Blk_Grd_Bg.jpg); width: 100%; height: 20px;
                    background-color: #000000">
                    <span class="ArialWhite12BLD">&nbsp;</span>
                </td>
            </tr>
            <tr>
                <td align="center" style="width: 100%" valign="top">
    
                
                 
    <div style="margin: 0px 7px 7px" >  </div>
                            <div>
                            <table width="100%">
                                                    <tr>
                                                        <td>
                                                        <div style="margin: 3px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold; padding-left: 20px;">
                                       My Corals
                                                            <asp:Label ID="LabelFilter" runat="server" ForeColor="Blue" />
                                                        </div>
                                                        </td>
                                                        <td align="right"><div style="padding-right: 30px">
                                                            <asp:LinkButton ID="LinkButtonAddCorals" runat="server" text="Add Corals" /></div></td>
                                                    </tr>
                                                </table> </div>
    
                                         <obout:Grid ID="GridMyCorals" runat="server" AllowFiltering="false" 
                                                       AutoGenerateColumns="False" FolderStyle="/Images/Grid/styles/black_glass" 
                                                       Width="612px" AllowAddingRecords="False" 
                                              EnableRecordHover="True" AllowGrouping="True" DataKeyNames="CoralsID"
                                Serialize="False"  DataSourceID="ObjectDataSourceCorals" >
                                       <ClientSideEvents OnBeforeClientDelete="GridMyCorals_Deleting" />      
                                             <Columns>
    
                                             <obout:Column ID="CoralsID" runat="server" DataField="CoralsID" HeaderText="  " width="1" Visible="false" />
                                               <obout:Column ID="Picture" runat="server" DataField="Picture" HeaderText="  " width="1" Visible="false" />
                                               <obout:Column ID="PictureThumb" runat="server" DataField="PictureThumb" HeaderText="  " width="1" Visible="false" />
    
                                                 <obout:Column ID="CoralsID2" runat="server" DataField="UserAccountsID,CoralsID" 
                                                     HeaderText="  " TemplateID="CoralsIDUserIdTemplate" width="170"/>
                                                    
                                                 <obout:Column  ID="Corals" runat="server" AllowDelete="true"
                                                     TemplateID="Coralstemplate" Width="800%" DataField="CoralsID, MembersCoralName, Corals, CoralGenus, CoralGenusID, CoralType, CoralTypeID, CoralSpecies, CoralSpeciesID,  Description, datestamp, UserAccountsID, NickName, CoralAvailability, Price, UserRating" 
                                                     HeaderText="Corals" Align="left" />
                                                   
                                                 
                                               
                                                 
                                                 <obout:Column ID="ViewCorals" DataField="Corals, Picture, PictureThumb" HeaderText="View Reviews" Width="150" runat="server" templateid="ViewCoralstemplate" />
                                                 
                                                 
                                             </Columns>
                                             <Templates>
                                                 <obout:GridTemplate ID="Coralstemplate" runat="server">
                                                     <Template>
                                                        
                                                          <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem("MembersCoralName")%>' Font-Size="Medium" /><br />
    
    
                                                 
                                                            <div style="padding: 6px 6px 6px 0px; text-align: left;"><%# Left(Container.DataItem("Description"), 200) & "..."%></div>
                                                            <b>Availability:</b> &nbsp;<%# GetAvailabilityFormat(Container.DataItem("CoralAvailability"))%>&nbsp;&nbsp;<b>Price:</b> &nbsp;$<%# Container.DataItem("Price")%>&nbsp;&nbsp; <br /> <b>Rating:</b> &nbsp;<%# GetRatingFormat(Container.DataItem("Rating"))%><br /><b><%# FormatDateTime(Container.DataItem("DateStamp") & "", DateFormat.LongDate)%></b><hr noshade="noshade" align="left" color="#CCCCCC" /><b>Type:</b>&nbsp;<asp:HyperLink ID="HyperLinkCoralType" runat="server" Text='<%# Container.DataItem("CoralType")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?CoraltypeID=" & Container.DataItem("CoralTypeID")%>' />&nbsp;&nbsp;<b>Genus:</b>&nbsp;<asp:HyperLink ID="HyperLinkGenusType" runat="server" Text='<%# Container.DataItem("CoralGenus")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?GenustypeID=" & Container.DataItem("CoralGenusID")%>' />&nbsp;&nbsp;
                                                         <br /><b>Species:</b> &nbsp;<asp:HyperLink ID="HyperLinkSpecies" runat="server" Text='<%# Container.DataItem("CoralSpecies")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?SpeciestypeID=" & Container.DataItem("CoralSpeciesID")%>' />
                                                         <br />
                                                         <table>
                                                            <tr>
                                                                <td> <asp:HyperLink ID="HyperLinkEdit" runat="server" text="Edit" NavigateUrl='<%# "/Corals/AddEditCoral.aspx?ID=" & Container.DataItem("CoralsID")%>' BorderColor="Black" BorderStyle="Solid" BorderWidth="1"><img src="/Images/ButtonEdit.jpg" /></asp:HyperLink> </td>
                                                                <td> <img src="/Images/ButtonDelete.jpg" onclick="GridMyCorals.delete_record(this);"  border="1" style="border: 1px solid #000000" />
                                                                
                                                              
                                                            </tr>
                                                         </table>
                                                        
                                                        
                                                     </Template>
                                                 </obout:GridTemplate>
                                                 
                                                 
                                                  <obout:GridTemplate ID="ViewCoralstemplate" runat="server">
                                                     <Template>
                                                      
                                                         
                                                         
                                                         <asp:Hyperlink ID="Imageicon" runat="server" Width="50" alt="View Details" NavigateUrl='<%# "/Corals/CoralDetails.aspx?ID=" & Container.DataItem("CoralsID")%>'><img src="/images/icons/fish.png" width="90" /></asp:Hyperlink>
                                                         
                                                        
    
                                                     </Template>
                                                 </obout:GridTemplate>
                                                 <obout:GridTemplate ID="CoralsIDUserIdTemplate" runat="server">
                                                     <Template>
                                                     <div style="padding: 6px; border: 1px solid #666666; text-align: center; background-color: #DEDEDE; background-image: url('/Images/Grey_Gradient_profile_bg.jpg');">
    
                                                         <obout:ImageZoom ID="ImageZoomPort" runat="server" StyleFolder="/Images/Imagezoom/Styles/dark" BorderColor="Black" BorderStyle="Solid" BorderWidth="1" />
    
                                                        <br />
                                                      
                                                         <asp:HiddenField ID="HiddenUserID" runat="server" Value='<%# Container.DataItem("UserAccountsID") %>' />
                                                         <asp:HiddenField ID="HiddenCoralsID" runat="server" Value='<%# Container.DataItem("CoralsID") %>' />
                                                        <asp:HiddenField ID="HiddenPicture" runat="server" Value='<%# Container.DataItem("Picture")%>' />
                                                     <asp:HiddenField ID="HiddenPictureThumb" runat="server" Value='<%# Container.DataItem("PictureThumb")%>' />
                                                     </div>
                                                     </Template>
                                                 </obout:GridTemplate>
                                                
                                             </Templates>
                                         </obout:Grid>
    
    
                                        
                     
    
    
    
    
                                         <asp:ObjectDataSource ID="ObjectDataSourceCorals" 
                         runat="server" SelectMethod="GetCoralsByUser" TypeName="DAL" DeleteMethod="DeleteCoral" >
                                             <DeleteParameters>
                                                 <asp:Parameter Name="CoralsID" Type="Int32" />
                                             </DeleteParameters>
                                             <SelectParameters>
                                                  <asp:SessionParameter Name="UserAccountsID" SessionField="UserAccountsID" 
                                                      Type="Int32" />
                                             </SelectParameters>
                     </asp:ObjectDataSource> 
    
    
    
                                        
                     
    
    
    
    
                                         <br />
            
                       </td>
            </tr>
        </table>
    
    
    
    
    .vb page
    
    Imports Obout.Grid
    Imports OboutInc.ImageZoom
    Imports System.IO
    
    Partial Class Corals_MyCorals
        Inherits System.Web.UI.Page
    
        Dim Dal As New DAL
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim Script As String = "function GridMyCorals_Deleting(sender) {" & _
    " return confirm('Are you sure you want to delete this Coral?');" & _
    " } "
    
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "Script", Script, True)
    
        End Sub
    
        Protected Sub GridMyCorals_DeleteCommand(ByVal sender As Object, ByVal e As Obout.Grid.GridRecordEventArgs) Handles GridMyCorals.DeleteCommand
    
            'ObjectDataSourceCorals.Dispose()
            'GridMyCorals.Dispose()
    
    
            Dim Picture = e.Record("Picture")
            Dim PictureThumb = e.Record("PictureThumb")
    
            Dim fd1 As New FileInfo(Server.MapPath("/" & Application("SysUserDir") & "/" & Picture))
            If fd1.Exists = True Then
                fd1.Delete()
            End If
    
            Dim fd2 As New FileInfo(Server.MapPath("/" & Application("SysUserDir") & "/" & PictureThumb))
            If fd2.Exists = True Then
                fd2.Delete()
            End If
    
            Dim CoralsID = e.Record("CoralsID")
            Dal.DeleteCoral(CoralsID)
    
         
        End Sub
    
        Protected Sub GridMyCorals_RowDataBound(ByVal sender As Object, ByVal e As Obout.Grid.GridRowEventArgs) Handles GridMyCorals.RowDataBound
    
            If e.Row.RowType = GridRowType.DataRow Then
    
                Dim cell As GridDataControlFieldCell = CType(e.Row.Cells(3), GridDataControlFieldCell)
                Dim CoralsID = CType(cell.FindControl("HiddenCoralsID"), HiddenField).Value
                Dim UserAccountsID = CType(cell.FindControl("HiddenUserID"), HiddenField).Value
    
    
                Dim Picture = CType(cell.FindControl("HiddenPicture"), HiddenField).Value
                Dim PictureThumb = CType(cell.FindControl("HiddenPictureThumb"), HiddenField).Value
    
    
    
                If IsDBNull(Picture) Or IsNothing(Picture) Or Picture = "" Then
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).ImageUrl = "/Images/NoPhoto.jpg"
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).Width = "125"
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).BigImageUrl = "/Images/NoPhotoLarge.jpg"
                Else
                    'Dim RecentMembersThumb = Dal.GetProfilePicture(UserID)
                    'scale Picture-----------------------------------------------------
    
                    Dim ImageScale As New ImageScale
    
                    ImageScale.MaxWidth = 125
                    ImageScale.MaxHeight = 125
    
                    ImageScale.PictureThumbLocation = "/" + Application("SysUserDir") + PictureThumb
    
                    ImageScale.ImageScaler()
    
                    'End Scale-------------------------------------------------------
    
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).ImageUrl = "/" + Application("SysUserDir") + PictureThumb
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).BigImageUrl = "/" + Application("SysUserDir") + Picture
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).Width = ImageScale.ScaledImageWidth
                    CType(cell.FindControl("ImageZoomPort"), ImageZoom).Height = ImageScale.ScaledImageHeight
    
                    Picture = Nothing
                    PictureThumb = Nothing
    
                End If
    
            End If
    
        End Sub
    
        Function GetAvailabilityFormat(ByVal CoralAvailability As String) As String
    
            If CoralAvailability = "Now" Then
                Return "<Font color=#006600>Now</font>"
            ElseIf CoralAvailability = "Soon" Then
                Return "<Font color=#996600>Soon</font>"
            End If
    
    
    
        End Function
    
        Function GetRatingFormat(ByVal userRating As Object) As String
            Dim decimalRating As Decimal = Nothing
    
            If TypeOf userRating Is Decimal Then
                decimalRating = DirectCast(userRating, Decimal)
            ElseIf IsNumeric(userRating) Then
                decimalRating = CDec(userRating)
            End If
    
            If decimalRating = Nothing Then
                Return ("Not Yet Rated")
            Else
                Return (Format(decimalRating, "0.0") & "%")
            End If
    
        End Function
    
        Protected Sub LinkButtonAddCorals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButtonAddCorals.Click
    
            Response.Redirect("/Corals/AddEditCoral.aspx")
    
        End Sub
    
        
    End Class
    line 187 is where it blows up

    Is there any way to solve this?

    I would think the dataset would load and close the data and that would be that.

    Any help would be greatly appreciated
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It sounds like the account that is running the ASP.NET application does not have permissions to delete the file.

    -Frinny

    Comment

    • vertigo262
      New Member
      • Jul 2007
      • 62

      #3
      If I delete them using the formview, it works no problem. but when the grid loads them, they won't delete

      Comment

      Working...