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