Jump to content

Trying to delete a file that's in use

- - - - -

  • Please log in to reply
2 replies to this topic

#1
TheWebGuy

TheWebGuy

    Newbie

  • Members
  • Pip
  • 7 posts
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\ReefJunkies\Users\ganjaman\Corals\Pict ure\20080827123704lobophytum388wfsmall.jpg' 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



   1.  

   2.  

   3. Aspx page

   4.  

   5.  

   6. <table border="0" cellpadding="0" cellspacing="0" width="100%">

   7.         <tr>

   8.             <td style="background-image: url(/Images/Blk_Grd_Bg.jpg); width: 100%; height: 20px;

   9.                 background-color: #000000">

  10.                 <span class="ArialWhite12BLD"> </span>

  11.             </td>

  12.         </tr>

  13.         <tr>

  14.             <td align="center" style="width: 100%" valign="top">

  15.  

  16.  

  17.  

  18. <div style="margin: 0px 7px 7px" >  </div>

  19.                         <div>

  20.                         <table width="100%">

  21.                                                 <tr>

  22.                                                     <td>

  23.                                                     <div style="margin: 3px; text-align: left; font-family: Arial, Helvetica, sans-serif; font-size: large; font-weight: bold; padding-left: 20px;">

  24.                                    My Corals

  25.                                                         <asp:Label ID="LabelFilter" runat="server" ForeColor="Blue" />

  26.                                                     </div>

  27.                                                     </td>

  28.                                                     <td align="right"><div style="padding-right: 30px">

  29.                                                         <asp:LinkButton ID="LinkButtonAddCorals" runat="server" text="Add Corals" /></div></td>

  30.                                                 </tr>

  31.                                             </table> </div>

  32.  

  33.                                      <obout:Grid ID="GridMyCorals" runat="server" AllowFiltering="false" 

  34.                                                    AutoGenerateColumns="False" FolderStyle="/Images/Grid/styles/black_glass" 

  35.                                                    Width="612px" AllowAddingRecords="False" 

  36.                                           EnableRecordHover="True" AllowGrouping="True" DataKeyNames="CoralsID"

  37.                             Serialize="False"  DataSourceID="ObjectDataSourceCorals" >

  38.                                    <ClientSideEvents OnBeforeClientDelete="GridMyCorals_Deleting" />      

  39.                                          <Columns>

  40.  

  41.                                          <obout:Column ID="CoralsID" runat="server" DataField="CoralsID" HeaderText="  " width="1" Visible="false" />

  42.                                            <obout:Column ID="Picture" runat="server" DataField="Picture" HeaderText="  " width="1" Visible="false" />

  43.                                            <obout:Column ID="PictureThumb" runat="server" DataField="PictureThumb" HeaderText="  " width="1" Visible="false" />

  44.  

  45.                                              <obout:Column ID="CoralsID2" runat="server" DataField="UserAccountsID,CoralsID" 

  46.                                                  HeaderText="  " TemplateID="CoralsIDUserIdTemplate" width="170"/>

  47.  

  48.                                              <obout:Column  ID="Corals" runat="server" AllowDelete="true"

  49.                                                  TemplateID="Coralstemplate" Width="800%" DataField="CoralsID, MembersCoralName, Corals, CoralGenus, CoralGenusID, CoralType, CoralTypeID, CoralSpecies, CoralSpeciesID,  Description, datestamp, UserAccountsID, NickName, CoralAvailability, Price, UserRating" 

  50.                                                  HeaderText="Corals" Align="left" />

  51.  

  52.  

  53.  

  54.  

  55.                                              <obout:Column ID="ViewCorals" DataField="Corals, Picture, PictureThumb" HeaderText="View Reviews" Width="150" runat="server" templateid="ViewCoralstemplate" />

  56.  

  57.  

  58.                                          </Columns>

  59.                                          <Templates>

  60.                                              <obout:GridTemplate ID="Coralstemplate" runat="server">

  61.                                                  <Template>

  62.  

  63.                                                       <asp:Label ID="Label1" runat="server" Text='<%# Container.DataItem("MembersCoralName")%>' Font-Size="Medium" /><br />

  64.  

  65.  

  66.  

  67.                                                         <div style="padding: 6px 6px 6px 0px; text-align: left;"><%# Left(Container.DataItem("Description"), 200) & "..."%></div>

  68.                                                         <b>Availability:</b>  <%# GetAvailabilityFormat(Container.DataItem("CoralAvailability"))%>  <b>Price:</b>  $<%# Container.DataItem("Price")%>   <br /> <b>Rating:</b>  <%# GetRatingFormat(Container.DataItem("Rating"))%><br /><b><%# FormatDateTime(Container.DataItem("DateStamp") & "", DateFormat.LongDate)%></b><hr noshade="noshade" align="left" color="#CCCCCC" /><b>Type:</b> <asp:HyperLink ID="HyperLinkCoralType" runat="server" Text='<%# Container.DataItem("CoralType")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?CoraltypeID=" & Container.DataItem("CoralTypeID")%>' />  <b>Genus:</b> <asp:HyperLink ID="HyperLinkGenusType" runat="server" Text='<%# Container.DataItem("CoralGenus")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?GenustypeID=" & Container.DataItem("CoralGenusID")%>' />  

  69.                                                      <br /><b>Species:</b>  <asp:HyperLink ID="HyperLinkSpecies" runat="server" Text='<%# Container.DataItem("CoralSpecies")%>' NavigateUrl='<%# "/Corals/RecentCorals.aspx?SpeciestypeID=" & Container.DataItem("CoralSpeciesID")%>' />

  70.                                                      <br />

  71.                                                      <table>

  72.                                                         <tr>

  73.                                                             <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>

  74.                                                             <td> <img src="/Images/ButtonDelete.jpg" onclick="GridMyCorals.delete_record(this);"  border="1" style="border: 1px solid #000000" />

  75.  

  76.  

  77.                                                         </tr>

  78.                                                      </table>

  79.  

  80.  

  81.                                                  </Template>

  82.                                              </obout:GridTemplate>

  83.  

  84.  

  85.                                               <obout:GridTemplate ID="ViewCoralstemplate" runat="server">

  86.                                                  <Template>

  87.  

  88.  

  89.  

  90.                                                      <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>

  91.  

  92.  

  93.  

  94.                                                  </Template>

  95.                                              </obout:GridTemplate>

  96.                                              <obout:GridTemplate ID="CoralsIDUserIdTemplate" runat="server">

  97.                                                  <Template>

  98.                                                  <div style="padding: 6px; border: 1px solid #666666; text-align: center; background-color: #DEDEDE; background-image: url('/Images/Grey_Gradient_profile_bg.jpg');">

  99.  

 100.                                                      <obout:ImageZoom ID="ImageZoomPort" runat="server" StyleFolder="/Images/Imagezoom/Styles/dark" BorderColor="Black" BorderStyle="Solid" BorderWidth="1" />

 101.  

 102.                                                     <br />

 103.  

 104.                                                      <asp:HiddenField ID="HiddenUserID" runat="server" Value='<%# Container.DataItem("UserAccountsID") %>' />

 105.                                                      <asp:HiddenField ID="HiddenCoralsID" runat="server" Value='<%# Container.DataItem("CoralsID") %>' />

 106.                                                     <asp:HiddenField ID="HiddenPicture" runat="server" Value='<%# Container.DataItem("Picture")%>' />

 107.                                                  <asp:HiddenField ID="HiddenPictureThumb" runat="server" Value='<%# Container.DataItem("PictureThumb")%>' />

 108.                                                  </div>

 109.                                                  </Template>

 110.                                              </obout:GridTemplate>

 111.  

 112.                                          </Templates>

 113.                                      </obout:Grid>

 114.  

 115.  

 116.  

 117.  

 118.  

 119.  

 120.  

 121.  

 122.                                      <asp:ObjectDataSource ID="ObjectDataSourceCorals" 

 123.                      runat="server" SelectMethod="GetCoralsByUser" TypeName="DAL" DeleteMethod="DeleteCoral" >

 124.                                          <DeleteParameters>

 125.                                              <asp:Parameter Name="CoralsID" Type="Int32" />

 126.                                          </DeleteParameters>

 127.                                          <SelectParameters>

 128.                                               <asp:SessionParameter Name="UserAccountsID" SessionField="UserAccountsID" 

 129.                                                   Type="Int32" />

 130.                                          </SelectParameters>

 131.                  </asp:ObjectDataSource> 

 132.  

 133.  

 134.  

 135.  

 136.  

 137.  

 138.  

 139.  

 140.  

 141.                                      <br />

 142.  

 143.                    </td>

 144.         </tr>

 145.     </table>

 146.  

 147.  

 148.  

 149.  

 150. .vb page

 151.  

 152. Imports Obout.Grid

 153. Imports OboutInc.ImageZoom

 154. Imports System.IO

 155.  

 156. Partial Class Corals_MyCorals

 157.     Inherits System.Web.UI.Page

 158.  

 159.     Dim Dal As New DAL

 160.  

 161.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

 162.  

 163.         Dim Script As String = "function GridMyCorals_Deleting(sender) {" & _

 164. " return confirm('Are you sure you want to delete this Coral?');" & _

 165. " } "

 166.  

 167.         Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "Script", Script, True)

 168.  

 169.     End Sub

 170.  

 171.     Protected Sub GridMyCorals_DeleteCommand(ByVal sender As Object, ByVal e As Obout.Grid.GridRecordEventArgs) Handles GridMyCorals.DeleteCommand

 172.  

 173.         'ObjectDataSourceCorals.Dispose()

 174.         'GridMyCorals.Dispose()

 175.  

 176.  

 177.         Dim Picture = e.Record("Picture")

 178.         Dim PictureThumb = e.Record("PictureThumb")

 179.  

 180.         Dim fd1 As New FileInfo(Server.MapPath("/" & Application("SysUserDir") & "/" & Picture))

 181.         If fd1.Exists = True Then

 182.             fd1.Delete()

 183.         End If

 184.  

 185.         Dim fd2 As New FileInfo(Server.MapPath("/" & Application("SysUserDir") & "/" & PictureThumb))

 186.         If fd2.Exists = True Then

 187.             fd2.Delete()

 188.         End If

 189.  

 190.         Dim CoralsID = e.Record("CoralsID")

 191.         Dal.DeleteCoral(CoralsID)

 192.  

 193.  

 194.     End Sub

 195.  

 196.     Protected Sub GridMyCorals_RowDataBound(ByVal sender As Object, ByVal e As Obout.Grid.GridRowEventArgs) Handles GridMyCorals.RowDataBound

 197.  

 198.         If e.Row.RowType = GridRowType.DataRow Then

 199.  

 200.             Dim cell As GridDataControlFieldCell = CType(e.Row.Cells(3), GridDataControlFieldCell)

 201.             Dim CoralsID = CType(cell.FindControl("HiddenCoralsID"), HiddenField).Value

 202.             Dim UserAccountsID = CType(cell.FindControl("HiddenUserID"), HiddenField).Value

 203.  

 204.  

 205.             Dim Picture = CType(cell.FindControl("HiddenPicture"), HiddenField).Value

 206.             Dim PictureThumb = CType(cell.FindControl("HiddenPictureThumb"), HiddenField).Value

 207.  

 208.  

 209.  

 210.             If IsDBNull(Picture) Or IsNothing(Picture) Or Picture = "" Then

 211.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).ImageUrl = "/Images/NoPhoto.jpg"

 212.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).Width = "125"

 213.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).BigImageUrl = "/Images/NoPhotoLarge.jpg"

 214.             Else

 215.                 'Dim RecentMembersThumb = Dal.GetProfilePicture(UserID)

 216.                 'scale Picture-----------------------------------------------------

 217.  

 218.                 Dim ImageScale As New ImageScale

 219.  

 220.                 ImageScale.MaxWidth = 125

 221.                 ImageScale.MaxHeight = 125

 222.  

 223.                 ImageScale.PictureThumbLocation = "/" + Application("SysUserDir") + PictureThumb

 224.  

 225.                 ImageScale.ImageScaler()

 226.  

 227.                 'End Scale-------------------------------------------------------

 228.  

 229.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).ImageUrl = "/" + Application("SysUserDir") + PictureThumb

 230.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).BigImageUrl = "/" + Application("SysUserDir") + Picture

 231.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).Width = ImageScale.ScaledImageWidth

 232.                 CType(cell.FindControl("ImageZoomPort"), ImageZoom).Height = ImageScale.ScaledImageHeight

 233.  

 234.                 Picture = Nothing

 235.                 PictureThumb = Nothing

 236.  

 237.             End If

 238.  

 239.         End If

 240.  

 241.     End Sub

 242.  

 243.     Function GetAvailabilityFormat(ByVal CoralAvailability As String) As String

 244.  

 245.         If CoralAvailability = "Now" Then

 246.             Return "<Font color=#006600>Now</font>"

 247.         ElseIf CoralAvailability = "Soon" Then

 248.             Return "<Font color=#996600>Soon</font>"

 249.         End If

 250.  

 251.  

 252.  

 253.     End Function

 254.  

 255.     Function GetRatingFormat(ByVal userRating As Object) As String

 256.         Dim decimalRating As Decimal = Nothing

 257.  

 258.         If TypeOf userRating Is Decimal Then

 259.             decimalRating = DirectCast(userRating, Decimal)

 260.         ElseIf IsNumeric(userRating) Then

 261.             decimalRating = CDec(userRating)

 262.         End If

 263.  

 264.         If decimalRating = Nothing Then

 265.             Return ("Not Yet Rated")

 266.         Else

 267.             Return (Format(decimalRating, "0.0") & "%")

 268.         End If

 269.  

 270.     End Function

 271.  

 272.     Protected Sub LinkButtonAddCorals_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButtonAddCorals.Click

 273.  

 274.         Response.Redirect("/Corals/AddEditCoral.aspx")

 275.  

 276.     End Sub

 277.  

 278.  

 279. End Class

 280.  

 281.  

 282.  




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

#2
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
i Found this post to be helpful in your case
http://forums.asp.net/t/1521711.aspx


#3
TheWebGuy

TheWebGuy

    Newbie

  • Members
  • Pip
  • 7 posts
Hi gokuajmes,

thanks for the link. Found it interesting and tried something similar.
Since I am using the obout.com grid. there is no itemdeleting.
instead, there is DeleteCommand

any ways, this is what I tried. Hoping it would work. I got the same error
"The process cannot access the file 'D:\InetPub\ReefJunkies\Users\Aquarium Dude\AquariumPictureGallery\Picture\aqua-7994.jpg' because it is being used by another process."

I will post the code anyways. I was hoping that would work.

i tried two variations


 Dim oImg As ImageButton = TryCast(GridAquariumsGalleryPictures.FindControl("ImageAquariumPicture"), ImageButton)


             If oImg IsNot Nothing Then

            Dim sUrl As String = oImg.ImageUrl

            File.Delete(Server.MapPath(sUrl))

        End If


   


and



   Dim oImg As ImageButton = TryCast("/" & Application("SysUserDir") & "/" & e.Record("PictureThumb"), ImageButton)


        If oImg IsNot Nothing Then

            Dim sUrl As String = oImg.ImageUrl

            File.Delete(Server.MapPath(sUrl))

        End If








1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users