1. The most basic requirement for the robustness of error (other than) handlers is the handling and capturing of program errors. In ASP.NET, error handling has the same mechanism as other programming languages. You can use Try...Catch...Finally and other methods. This is a great improvement compared with ASP. Moreover, using these error handling methods can greatly improve the readability of the program and the speed of program debugging. When these advantages are combined, we should pay more attention to this.
Regarding error handling, we can refer to this article (English):
http://www.123aspx.com/redir.aspx?res=28336
2. String processing In web design, string processing is almost the most common. After using ASP.NET, string processing is faster than ASP. Moreover, in ASP.NET, a string processing class StringBulider is specially added. Use this class to complete some common string operations, and the most important thing is, Using StringBuilder can greatly improve the speed of string processing.
In ASP.NET, the most common thing is to use "&" to connect two strings:
Dim myOutputString As String = "My name is"
Dim myInputString As String = "Alex"
myOutputString = myOutputString & myInputString
Response.Write(myoutputString)
Now, let's take a look at the use of StringBuilder. When using StringBuilder, we can do some basic operations on strings, such as Append, Replace, Insert, Remove, etc. Now let's look at specific examples.
(1) Use of Append in StringBuilder
Append is the same as Append in other languages, which is to add other characters at the end of the string.
Dim sb as StringBuilder = New StringBuilder()
sb.append( "<table border='1' width='80%'>" )
For i = 0 To RowCount - 1
sb.Append("<tr>")
For k = 0 To ColCount - 1
sb.Append("<td>")
sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())
sb.Append( "</td>" )
Next
sb.Append("<tr>")
Next
sb.Append( "</table>")
Dim strOutput as String = sb.ToString()
lblCompany.Text = strOutput
In the above program, the Append method is used to realize the output of a table. One thing to note is that the StringBulider must first use the ToString() method to convert it into the String type before it can be output directly. In the above examples, all we see is Append a direct string. In fact, this method has a very convenient function, that is, it can directly Append other types of variables, such as directly Appemd an Integer type value. Of course, our output will be automatically converted into a string afterward:
Sub Page_Load(Source As object, E As EventArgs)
Dim sb As System.Text.StringBuilder
Dim varother As Integer
varother=9999
sb =new System.Text.StringBuilder()
sb.append("<font color='blue'>You can Append other types: </font>")
sb.append(varother)
Response.write(sb.toString())
End Sub
(2) Use of other methods in strings We can also use other methods. Let’s take a look at the common ones:
Insert method can insert other characters at the specified position. Usage method: Insert (insert position, insert character);
Remove method can delete specified alphanumeric characters at a specified position. Usage method: Remove (actual position, number of characters);
Replace method can replace specified characters. Usage method: replace (replaced string, replacement string)
For a detailed introduction and usage of strings, please refer to the following article (in English):
http://aspfree.com/aspnet/stringbuilder.aspx
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp
3. Closing the database link Connection and DataReader When using ASP programming, we already know that after using the database connection, the connection must be closed and then set to NoThing. In Asp.NET, we still need to use it this way. However, in ASP.NET, due to the use of ADO.NET, there are actually some subtle differences in some related processing aspects, and these differences are often also This is what we need to pay most attention to when designing. Now, let's take an example to see what issues need to be paid attention to in common ADO.NET operations.
(1) Example 1
Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
Dim myDataReader As SqlDataReader
Try
myConnection.Open()
myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
dropDownList1.DataSource = myDataReader
dropDownList1.DataBind()
Catch myException As Exception
Response.Write("An error has occurred: " & myException.ToString())
Finally
If Not myDataReader Is Nothing Then
'Close DataReader
myDataReader.Close()
End If
End Try
In the above example, we noticed that only the DataReader was closed, not the Connection. Why? Carefully observe the above ExecuteReader method. It turns out that the ExecuteReader parameters are set. After ExecuteReader is executed, the Connection will be automatically closed. Therefore, after setting this up, there is no need to manually close the Connection.
(2) Example 2
Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))
Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)
Try
myConnection.Open()
dropDownList1.DataSource = myCommand.ExecuteReader()
dropDownList1.DataBind()
Catch myException As Exception
Response.Write("An error has occurred: " & myException.ToString())
Finally
If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then
myConnection.Close()
End If
End Try
In the above example, we found that the DataReader was not closed. Why? In fact, in the above code, the DataReader object is not directly generated, and of course there is no way to close it. One thing to note is that before closing the Connection, the program first determines whether the Connection is open. If not, there is no need to close it.
4. Use Web.Config/Maching.Config to save commonly used data. Some data we need to use frequently. For example, when using ADO.NET, the most common one is the database connection statement. In ASP, we often save this information in Application. Of course, this can also be done in ASP.NET. However, ASP.NET already provides a configuration file WEB.Config, so we'd better save this information in WEB.Config. Of course, we can also save it in Machine. Config, however, in this case, the entire website must be used, so generally we use Web.Config. Now, let's look at the specific use of this file.
(1) Web.Config file settings First, let’s look at the Web.Config settings. We add the following two items in this file. The settings are as follows:
<configuration>
<appsettings>
<add key="dsn" value="myserver"/>
<add key="someotherkey" value="somevalue"/>
</appsettings>
</configuration>
(2) Use of variables The above XML file sets two variables, dsn and someotherkey. Now let’s see how to use them in the program:
<html>
<script language="VB" runat=server>
Sub Page_Load(Sender as object, E as EventArgs)
Dim AppSettings as Hashtable = Context.GetConfig("appsettings")
DSN.Text = AppSettings("dsn")
SomeOther.Text = AppSettings("someotherkey")
End Sub
</script>
<body>
DSN Setting: <asp:label id="DSN" runat=server/> <br>
Some Other Setting: <asp:label id="SomeOther" runat=server/>
</body>
</html>
We see in the above program that using variables defined in this way is very simple and convenient.
5. Use .NET to debug the program
Debugging ASP programs has always been the most difficult part of writing ASP. ASP programmers probably have a deep understanding of this, because everyone uses Response.write to debug. The biggest disadvantage of this kind of debugging is that when we finish debugging, we must delete or comment out the information one by one. Think about it, if the program code reaches hundreds of lines or a program with many pages, how boring this kind of work will be. What I am most afraid of is One thing, if you forget to delete these debugging writes, some indecent debugging information may appear when the user uses it.
After using ASP.NET, we can directly define Trace to debug the program. The troubles mentioned above can be easily solved. If you are familiar with it, Trace can be implemented through specific pages and in the Web.Config configuration file. In this way, after the program is debugged, just set Trace to Off directly. In this way, the program will There will be no debugging functionality.
(1) Implementation of page debugging When a specific page needs to implement the debugging function, we can set it up like this:
<%@ Page Language="VB" Trace="True" %>
(2) Define WEB.Config and implement it in WEB.CONFIG. We can also enable program debugging:
<configuration>
<system.web>
<trace enabled="true" requestLimit="10" localOnly="false"/>
</system.web>
</configuration>
After using the above settings to turn on Trace, we can use Trace to debug the program in a specific program, such as:
Trace.Write("This is some custom debugging information")
Or debugger variables:
Trace.Write("This is is my variable and it's value is:" & myVariable.ToString())
From the above settings, we can see that in ASP.NET, the program debugging function is already very convenient and simple.