I have been learning Asp.net for a year. Although it cannot be called a great god, it also has a little knowledge accumulation. Let's get started with debugging.ArticleFor those who are confused about getting started. I hope you have learned this article to find the programmer from your confused life.
ForProgramThe employee said he could never escape the debugging threshold. I once remembered that I was a freshman. I learned that C was under VC.CodeCalculate the day of each month to find the day of the year. This is a very simple question. I think everyone will have ideas, but my friend's code has been written and compiled and run, but the result is incorrect. I didn't find any problems with his code. What makes me feel like this? So I immediately set a breakpoint and ran it, the problem was solved in less than five minutes after one step of debugging. The problem is that he adds more days of the month, that is, an equal sign is added to the for statement of the month. This small problem is not easy to find. This example shows how important debugging is for programmers. My exclusive words: people who do not debug will never program.
Now let's get down to the truth. The purpose of this article is to tell programmers who develop Asp.net how to debug and find the problem.
Debugging Skills 1:
The most formal debugging is to work with the system's breakpoint debugging function. First, we create a new page. For convenience, I directly write the code in the page_load function of the page. The example I wrote here is to show that a month or Day is the day of the year. The Code is as follows:
Protected Void Page_load ( Object Sender, eventargs e ){ // Assume that this year is a year, and the number of days in each month is listed by enumeration. The day of a month is the day of the day. // First, we will briefly test whether the input data is correct for the total number of days. Int32 [] months = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 ,30 , 31 , 30 , 31 }; // Use the LINQ method to sum Months. sum ();}
We first find this page in the solution and right-click this page to set it as the start page.
Set the breakpoint at the left of the Code Writing Page.
All the work has been completed. Let's start debugging. Press F5,
Our program has entered the debugging page as follows:
Add a yellow arrow in the red to indicate that the program is debugged here. Note that the yellow sentence has not been executed. For the debugging command, note the subsequent execution of F5 to the next breakpoint. No matter what code is in the middle, the program is executed to the next breakpoint in sequence. If there is no breakpoint, it will jump directly to the running status. The single-sentence execution and Debugging commands in F10 are skipped if you call a function or the like. If you think there is a problem with the function, press F11. If your function is very confident, press F10 to skip function debugging. I personally think the only difference between F11 and F11 is whether to debug the function.
Click F10 for debugging to jump to the next debugging point. It indicates that all the statements above the diving point are executed.
Let's take a look at the result of months. sum. Right-click the months variable and select quick view.
Enter the data you want to view, for example, the sum of months.
If the value is 365, the data is correct. In fact, there is another way. View our data
Find a wattings dialog box at the bottom and click it,
Then input months. sum () in his name to display the data we need on the right.
The formal debugging method is written here.
Debugging Skills 2:
Use the response. Write method. I think a lot of friends from ASP and PHP are used to this method. For example, if we want to check the sum of the data, we can directly write the result response. Write.
When debugging with this method, remember to add a return after response. Write. The reason is very simple, that is, you want to display all the statements you run in this place. Usage: This debugging method is helpful for debugging errors on the server. Because the development environment does not need to be installed on the server, we can only display the code before the error to see where there is a problem. (I have also encountered such an example before. The dynamic menu I wrote is to display different menu options based on permissions. I have written these statements and integrated them into my colleagues. It was okay, but after three days the problem came. I also said that there was a problem on the server, but I could not debug it myself, fortunately, a colleague who has been working for 10 years came to help me with response. the Write statement outputs the SQL statement and finds that the in the SQL statement is empty. If there is no data, an error is returned. If my colleagues didn't, I wrote a data script to run on the server, at that time, I also grew up ).
Debugging skills 3:
This method is not really a debugging solution, because the debugging needs to be wrong with the database. People who have developed projects know that common errors occur when they interact with the database. In comparison, when the length of data inserted exceeds the length specified by the database, then an error occurs. Well, I should tell you how to debug it. First, open the SQL Server Profiler software. If your SQL does not have its own, find the website and install it.
Then select create trail. Icon under the File menu. Enter the user name and password to log on. In the select event dialog box, select the RPC: Completed, SQL: batchcompleted, and SQL: batchstarting options, and then select the run dialog box.
At this time, we will try to see what we found in Asp.net and database operations ~
The statement just executed is captured, haha
Isn't it amazing? Here are some points worth attention. Whether it's a database operation or Asp.net's interaction with the database, this SQL Server Profiler will be captured. In fact, if you open this page for a long time and there will be a lot of data, you don't want to find so many pieces of data during debugging. First, clear the tracking records, the statement for running the code is displayed.
Of course, this SQL Server Profiler is actually not useful in this way. It is used for SQL optimization. I am only a little bit useful here. I hope Microsoft will not ask me for help.
It is written here. Sharing is a pleasure, but the sharer hopes to get more support from readers!