INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Delete an image not working properly

Delete an image not working properly

(OP)
I have a click event which is supposed to delete an image. I have a dropdown which displays all the images in a folder. When I try and use the dropdown selected value, the image is not deleted. However, if I pass the name of an image, the image is deleted. In the code below, when I hard code the image name ( commented out below ) the code works, but when I use the dropdown value it doesn't.

Any ideas?

mike

CODE

Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            'Dim ImageName = "test1thumb.png"
            Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

            Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

            If System.IO.File.Exists(ImagePath) Then
                System.IO.File.Delete(ImagePath)
            End If

        End If

        UpdatePanelImageManager.Update()


    End Sub 

RE: Delete an image not working properly


Have you verified that the path generated in this line:

Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

is indeed valid?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Delete an image not working properly

(OP)
Thank you for taking the time to reply.

Yes, the path is valid. The way I verified the path was to set the value of ImageName to a hard coded value:

Dim ImageName = "test1thumb.png"

When I set and use the above value for ImageName, the path works and the image is deleted.

I've even tried creating a bitmap variable set to an image, but that didn't work either.

mike

RE: Delete an image not working properly


No, I mean when the image name is retrieved from the dropdown, have you stepped through the code and made sure that ImagePath is correct (e.g., no spaces, extra backslashes, etc.)?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!

RE: Delete an image not working properly

(OP)
Yes, the path is ok. There are no errant spaces or extraneous characters. I am going to work on this some more, and I'll post back an update.

RE: Delete an image not working properly

(OP)
Ok, when I select an image from the dropdown list and attempt to delete it I get the following error message when the delete is in a try catch block:

"The process cannot access the file 'D:\sites\gallery\test1thumb.png' because it is being used by another process."

If I hard code the image name and do not select the image from the drop down, the image is deleted. However, even if the image is hard coded (as in the code below), and I select the hard coded image from the drop down, I get the above error. With the below code, as long as I do not select the same image in the drop down, the image is deleted, but if I select it I get the error. If I comment out the hard coded ImageName and uncomment out the ImageName set to the selected value in the drop down, I get the error.

So is it possible to unlock/close/dispose the image? Do I have to read the image into a file stream?

Here is my amended code with the try catch block:

CODE

Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            Try


                Dim ImageName = "test1thumb.png"
                'Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

                Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

                If System.IO.File.Exists(ImagePath) Then
                    System.IO.File.Delete(ImagePath)
                End If

            Catch ex As Exception
                errorlabel.Text = ErrorToString()
            End Try

        End If

        UpdatePanelImageManager.Update()


    End Sub 

RE: Delete an image not working properly

(OP)
I solved the issue. In another portion of my web app, I was creating a variable as a new image bitmap, and when I was done with the image (getting height and width) I should have disposed the variable. In this case, the variable was using the same drop down. So because I did not dispose of the bitmap object, when I attempted to delete the image, I was getting the error.

So, the solution was to use Dispose() to unlock the image.

The new bit map was being created in a SelectedIndexChanged event.

CODE

Protected Sub imagemanager_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        imagemanagerurl.Text = "<img src='gallery/" & imagemanagerdropdown.SelectedValue.ToString & "'>"

        'Dim image1 As Bitmap

        image1 = New Bitmap("D:\gallery\" & imagemanagerdropdown.SelectedValue.ToString, True)

        imgwidth.Text = "Width: " & image1.Width
        imgheight.Text = "Height: " & Image1.Height

        Image1.Dispose()

        UpdatePanelImageManager.Update() 

The Image1.Dispose() was not there when I was getting the error in the following code.

CODE

Protected Sub godeleteimage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles godeleteimage.Click

        If deleteimage.Checked = True Then

            Try

                'Dim ImageName = "test1thumb.png"
                Dim ImageName = imagemanagerdropdown.SelectedValue.ToString

                Dim ImagePath As [String] = [String].Format("{0}\{1}", Server.MapPath("gallery"), ImageName)

                If System.IO.File.Exists(ImagePath) Then
                    System.IO.File.Delete(ImagePath)
                End If

            Catch ex As Exception
                imagemanagerurl.Text = ErrorToString()
            End Try

        End If

        UpdatePanelImageManager.Update()


    End Sub 

Once the bitmap was being disposed by adding the Image1.Dispose(), my image delete functionality works great.

mike

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close