Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Try Catch - strange behavior

Status
Not open for further replies.

AndyH1

Programmer
Jan 11, 2004
350
GB
I wonder if anyone can advise me on some strange behavior I'm getting in some code using a Try catch statement. Its probably my lack of understanding. Maybe its something to do with threads.

I have a form in which I upload an image. This is done in the fl.UploadFile routine I wrote and this seems fine and uploads the image. The problem is I have some code later on in the catch part of the statemnt which deletes the image if the rest of the data is not inserted in the db correctly (dgProducts.UpdateOption adds the data). The problem is that everything seems to work, the image is uploaded and the data is entered in the database correctly and I then redirect to the next page, BUT the statement fl.DeleteFile (which deletes the file) is always being activated. I know this because if I comment it out the image is in the directory when I look, but not if I don't comment it out.

I can't figure out how this happens as the page redirects to AddRelationshipstoItems.aspx page correctly and a number is passed with this indicating the update was successful.

If I comment out the redirect the message I get is that insert was successful and Iv'e tried comment out the try catch and associated code and still get no exception being raised.

I have tried the putting the delete code in the 'Else' part of the 'If IsNumeric(result) Then' statement and that works fine and is a temporary solution, but it would not cover all exceptions so I really need to work out whats going on. can anyone help

Thanks
Andy


Sub UpdateOption(ByVal Source As Object, ByVal e As EventArgs)

Dim strImageDirectory As String
Dim bSucceeded As Boolean
Dim strFileName As String

' in final version should be Server.MapPath("/WER/CatalogueImages" + "\"
strImageDirectory = "C:\Documents and Settings\se1449\My Documents\Visual Studio 2005\WebSites\WER\CatalogueImages\"

bSucceeded = True
strFileName = ""
lblMsg.Text = ""
lblMsg.ForeColor = Drawing.Color.Black
' If filename then upload the image
Dim fl As New FileRoutines.FileUpload
If Not (myFile.Value = "") Then
strFileName = fl.UploadFile(myFile.PostedFile, strImageDirectory, System.IO.Path.GetFileName(myFile.PostedFile.FileName))
If strFileName = "" Then
bSucceeded = False
lblMsg.ForeColor = Drawing.Color.Red
lblMsg.Text = "There was an error uploading the image"
End If
End If
If bSucceeded Then ' if image was successfully uploaded
Try
Dim result As String
Dim dgProducts As New MiddleTier.SercoCatalogueDB
' Add data to the database
result = dgProducts.UpdateOption("Update", txtID.Value, txtProductCode.Text, txtDescription.Text, txtPrice.Text, lstTypeList.SelectedValue, txtComments.Text, strFileName)
' If successfully added details a numeric value will be retunred
' Equal to the ID of the record
If IsNumeric(result) Then
lblMsg.ForeColor = Drawing.Color.Green
lblMsg.Text = "Update success"
Response.Redirect("AddRelationshipstoItems.aspx?ID=" + result)
Else
lblMsg.ForeColor = Drawing.Color.Red
End If
Catch Exp As Exception
lblMsg.ForeColor = Drawing.Color.Red
lblMsg.Text = Exp.Message
' If failed to add entry need to also delete the image if one has been uploaded
If Not (strFileName = "") Then
' If file exists then delete it - in case of database exception
fl.DeleteFile(strImageDirectory + strFileName)
End If

End Try
End If
End Sub
 
right off the bat this is a problem
Code:
' in final version should be Server.MapPath("/WER/CatalogueImages" + "\"
        strImageDirectory = "C:\Documents and Settings\se1449\My Documents\Visual Studio 2005\WebSites\WER\CatalogueImages\"
not with the current problem, but since this needs to be configurable between environments don't hard code it. including your development box.

use the AppDomain.Current.BaseDirectory as the starting point to create the directory.

As for the non-working/non-functioning error.
1. don't catch Exception, catch a specific exception. Like a AdoException or something specific to the database.
2. use a transaction and just rollback on error. (useless the db is access, in which case you have bigger problems)
3. your swallowing your exception, so you have no idea why it's failing. You need to 1. log the exception and then process the exception. if all your doing is logging then let the exception bubble up.
4. you shouldn't do heavy processing in the catch block, you should just clean up resources and get out.

to debug this remove all your try/catches and fix the problem. Then go back and apply try/catch as necessary. (For example, if you don't know how it could fail, then you shouldn't catch.)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top