views:

249

answers:

2

I have two if statements and my project sees one and not the other. Here is the code:

If (IsPostBack) Then
  HandleUploadedFile()
End If

Dim savePath As String = "Images\ "

If (fileUpload.HasFile) Then
  Dim fileName As String = fileUpload.FileName
  savePath = Server.MapPath(savePath) + fileName
  fileUpload.SaveAs(savePath)
  Me.Label1.Text = "Your file was saved as " & fileName
  adp.Insert(fileUpload.FileName)
  Me.Label1.Text = "You did not specify a file to upload."
End If

When I trace my code I find it goes to the second if and then go to end if without running the code inside the conditional.

+1  A: 

What's happening in HandleUploadedFile?

If an error isn't trapped - you might fall right out of your routine (never reaching the second if).

If fileUpload is not declared... same scenario.

When you step through do you see it jumping to the end?

Edit: Will you really have a file upload when the page has NOT been posted back? Maybe you really mean to put the entire code block in the If IsPostBack code block?

Edit2: You could just add:
Public Sub HandleUploadedFile
above this line:
Dim savePath As String = "Images\ "

And add:
End Sub
below this line:
End If

Jeff Olson
OH yes - I also meant to mention (but dare not edit my answer again) that you should probably kill the trailing space in the value you are assigning to savePath or you will be trying to track another issue down.
Jeff Olson
why daren't you edit?
Joe Koberg
+1  A: 

Not sure if I'm understanding the question correctly, but are you sure that fileUpload.HasFile is true?

Davy8