Answered by:
what happens when a breakpoint is reached in code-behind

Question
-
User1955412872 posted
This may sound like an unusual question, but I need to know what is happening behind the scenes when I hit a breakpoint in the code behind my asp.net web page?
I ask because I have a situation where the code works correctly if I hit a breakpoint, but if I remove or deactivate the breakpoint, it does not work. I am looking for some kind of command that I can put in there that will give the same effect as the breakpoint activating. Problem is, I don't really understand the internal happenings when the breakpoint activates.
Monday, June 17, 2019 2:30 PM
Answers
-
User753101303 posted
So it seems you have two http requests being triggered (one when you click on "Choose file" and one when you selected a file). And so it seems that using a breakpoint allows basically to ignore this first http query.
I would use F12 Network in the browser to see all http queries and in particular what is triggering the first one which is not expected. With what you shown I would expect a single http query once you selected a file.Not directly related but I'm not sure this design is really convenient for users. You save them a click but on the other hand it's much more annoying for users if they selected a wrong file and can't fix that before triggering the actual upload. I would perhaps use that only if I expect them to upload small files (and you should likely have a UI allowing to then delete the file and upload the right one instead)...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 19, 2019 2:10 PM
All replies
-
User475983607 posted
This may sound like an unusual question, but I need to know what is happening behind the scenes when I hit a breakpoint in the code behind my asp.net web page?
I ask because I have a situation where the code works correctly if I hit a breakpoint, but if I remove or deactivate the breakpoint, it does not work. I am looking for some kind of command that I can put in there that will give the same effect as the breakpoint activating. Problem is, I don't really understand the internal happenings when the breakpoint activates.
A breakpoint stops execution at the breakpoint. The symptom usually indicates a timing issue in the code where setting a break point provides enough time for an asynchronous operation to complete.
Monday, June 17, 2019 3:48 PM -
User1955412872 posted
I originally thought it might be a timing issue too, but following that logic, I put in a MsgBox thinking that would produce the same results but it did not.
Monday, June 17, 2019 10:23 PM -
User-1174608757 posted
Hi GDFoster,
In fact, we often use breakpoint to debug code,for example, you may want to see the state of code variables or look at the call stack at a certain breakpoint .When set a breakpoint on a line , then you debug the applicaiton, it will just keep running until it meets the breakpoint. It will just stop and you could see the value of variable above the code.Which you could see as below:
So ,when you add a breakpoint in code behind, it will just keep running and stop at the line of breakpoint. You could see the value of variable above the point and you can also execute the code line by line by entering F10.
Here is the link,I hope it could help you.
https://docs.microsoft.com/en-us/visualstudio/debugger/using-breakpoints?view=vs-2019
Best Regards
Wei
Tuesday, June 18, 2019 2:19 AM -
User1955412872 posted
I understand how to use breakpoints, but I am trying to ask what is going on behind the scenes - how is control being transferred from the aspx page to the vb code?
The reason I ask is that I have a web page with a FileUploader control on it. When I put a breakpoint in my code, it works perfectly. When I disable the breakpoint (without making any other changes), it never selects the file.
So, I am trying to figure out what that breakpoint is doing to the executing code that causes it to work properly. It also works if I take the breakpoint out and put in a MsgBox. I feel like it has something to do with the transfer of control from the web page to the code behind.
Tuesday, June 18, 2019 3:05 PM -
User475983607 posted
I understand how to use breakpoints, but I am trying to ask what is going on behind the scenes - how is control being transferred from the aspx page to the vb code?
The reason I ask is that I have a web page with a FileUploader control on it. When I put a breakpoint in my code, it works perfectly. When I disable the breakpoint (without making any other changes), it never selects the file.
So, I am trying to figure out what that breakpoint is doing to the executing code that causes it to work properly. It also works if I take the breakpoint out and put in a MsgBox. I feel like it has something to do with the transfer of control from the web page to the code behind.
Share the source code or code that reproduces the issue if you need community assistance.
Tuesday, June 18, 2019 3:39 PM -
User753101303 posted
Hi,
it never selects the fileNot sure what you mean. What happens ? Do you have an error message ? As pointed already show us the code. For now my guess is that you have something such as both an async file upload and a full postback (in which you assume the async file upload ended previously ?)
Tuesday, June 18, 2019 3:49 PM -
User1955412872 posted
Nope- no error messages. If I have the breakpoint, I hit continue and it finishes saving the info to the database along with the file name.
If there is no breakpoint it is like I never even selected a file, but again, no error.
Tuesday, June 18, 2019 3:59 PM -
User753101303 posted
And what about showing your code ?
Tuesday, June 18, 2019 4:14 PM -
User1955412872 posted
Here is what my code looks like:
----------------- ASP.net code
<script type="text/javascript">function FileUploadComp() {
var AttID = '<%=AttachmentID%>';
__doPostBack('FileUpload1', AttID);}
</script>
<asp:FileUpload ID="FileUpload1" runat="server" onchange="FileUploadComp();"/>------------------ VB.net Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
....
Else 'If Not IsPostBack
Dim eventTarget As String
Dim eventArgument As StringIf (Me.Request("__EVENTTARGET") Is Nothing) Then
eventTarget = String.Empty
Else
eventTarget = Me.Request("__EVENTTARGET")
End IfIf (Me.Request("__EVENTARGUMENT") Is Nothing) Then
eventArgument = String.Empty
Else
eventArgument = Me.Request("__EVENTARGUMENT")
End If
Dim valuePassed As String = eventArgumentFileUploadComplete()
End If 'If Not IsPostBack
End Sub
Sub FileUploadComplete()
'This is where I put the breakpoint
debugLine = debugLine + " 100 "If FileUpload1.HasFile Then
..... this is where the work is done uploading the info from the file
End Sub
Tuesday, June 18, 2019 4:33 PM -
User1955412872 posted
Also, I have tried moving the breakpoint or MsgBox around to different places in the code. It always works as long as things happen in this order:
1) I click on the "Choose File" button of the FileUploader
2) THe breakpoint is hit or the MsgBox appears - at this point I DO NOT click continue or OK
3) I select the file and click Open
4) I click Continue or the OK button
Note that if I click Continue or OK before choosing the file the FileUploader does not contain a file when it returns to the code.
Tuesday, June 18, 2019 4:52 PM -
User475983607 posted
The code has design bugs. The MsgBox appears because the browser made a valid request but the file has not been selected and therefore the file is not part of the request stream. I recommend doing a standard file upload.
If you need to upload the file using JavaScript, then use JavaScript and FormData.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
Also, MsgBox is not a valid troubleshooting tool in web applications. The reason the breakpoint works is it always enough time to actually start the file upload.
Tuesday, June 18, 2019 5:34 PM -
User753101303 posted
So it seems you have two http requests being triggered (one when you click on "Choose file" and one when you selected a file). And so it seems that using a breakpoint allows basically to ignore this first http query.
I would use F12 Network in the browser to see all http queries and in particular what is triggering the first one which is not expected. With what you shown I would expect a single http query once you selected a file.Not directly related but I'm not sure this design is really convenient for users. You save them a click but on the other hand it's much more annoying for users if they selected a wrong file and can't fix that before triggering the actual upload. I would perhaps use that only if I expect them to upload small files (and you should likely have a UI allowing to then delete the file and upload the right one instead)...
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, June 19, 2019 2:10 PM