ASP. NET Programming I. Handling of errors)
The most basic requirement for program robustness is the processing and capturing of program errors. in ASP. NET, the error processing mechanism is the same as that in other programming languages. You can use Try... Catch... Finally and so on, which is greatly improved compared with ASP. In addition, using these error handling methods can greatly improve the readability and debugging speed of the program. When combining these advantages, we should pay more attention to this.
ASP. NET Programming II. string processing
In web design, string processing is almost the most common. Use ASP.. NET, the string processing speed is faster than ASP. NET, specifically added a string processing class StringBulider, using this class can complete some common string operations, and most importantly, using StringBuilder can greatly improve the string processing speed.
In ASP. NET, the most common 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 perform some basic operations on strings, such as Append, Replace, Insert, and Remove, now let's take a look at the specific example.
1) Use of Append in StringBuilder
Append is the same as Append in other languages, that is, adding 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 output a table. Note that the StringBulider must first use ToString () the method can be converted to the String type for direct output. In the above example, all we see is a direct string of Append. In fact, this method has a very convenient function, that is, it can directly Append other types of variables, for example, you can Appemd a value of the Integer type directly. Of course, the output will be automatically converted into a string:
- Sub Page_Load (Source As object, E As EventArgs)
- Dim sb As System. Text. StringBuilder
- Dim varother As Integer
- Varothers = 9999
- Sb =NewSystem. Text. StringBuilder ()
- Sb. append ("<Font color = 'blue'> 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 to look at the common ones:
Insert method. you can Insert other characters at the specified position. Use this method: Insert position, Insert character );
The Remove method can be used to delete a specified number of characters at a specified position. Usage: Remove the actual position, number of characters );
The Replace method can replace a specified character. Usage: Replace is replaced by a string)
ASP. NET Programming 3. database Connection and DataReader Shutdown
When programming with ASP, we know that after connecting to a database, we must close the connection and set it to NoThing. In Asp. NET, we still need to use it like this, however, in ASP. NET, due to the use of ADO. NET. Therefore, there are some subtle differences in some related processing. These differences are often the most important during our design. 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 =NewSqlConnection (ConfigurationSettings. receivettings ("DSN_pubs"))
- Dim myCommand As SqlCommand =NewSqlCommand ("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 preceding example, we noticed that only DataReader is disabled and Connection is not disabled. Why? Observe the preceding ExecuteReader method carefully. Originally, the ExecuteReader parameter is set. After the ExecuteReader is executed, the Connection is automatically closed. Therefore, after this setting, 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 DataReader was not closed. Why? In fact, the above Code does not directly generate the DataReader object, and of course it cannot be closed. Note that before closing the Connection, the program first checks whether the Connection is enabled. If the Connection is not enabled, it is unnecessary to close the Connection.
ASP. NET Programming 4. Use Web. Config/Maching. Config to save common data
Some data needs to be used frequently. For example, when using ADO. NET, the most common is the database connection statement. In ASP, we often save this information in the Application. Of course, in ASP. NET. However, ASP. NET has provided a configuration file WEB. config, so we 'd better save the information on the WEB. config, of course, we can also save it in Machine. config. However, in this case, the entire website must be used. Therefore, we generally use Web. config. Now, let's take a look at the usage of this file.
1) settings of the Web. Config file
First, let's take a look at the settings of Web. Config. We Add the following two items in this file:
- ﹤configuration﹥
- ﹤appsettings﹥
- ﹤add key="dsn" value="myserver"/﹥
- ﹤add key="someotherkey" value="somevalue"/﹥
- ﹤/appsettings﹥
- ﹤/configuration﹥
(2) Use of Variables
The above XML file sets the dsn and someotherkey variables. 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﹥
The above program shows that using the variables defined in this way is very simple and convenient.
ASP. NET Programming 5. debug the program using. NET
Debugging ASP programs has always been the most difficult part to compile ASP programs. ASP programmers may have a deep understanding of this point, because everyone uses Response. write for debugging. The biggest drawback of such debugging is that when debugging is completed, we must delete or comment out the information one by one. Think about it. If the program code Reaches hundreds of lines or many pages, this kind of work is so boring and boring. If you forget to delete the write for debugging, some indecent debugging information may appear when you use it.
After ASP. NET is used, we can directly define Trace to debug the program. The above mentioned troubles can be easily solved. Familiar with Trace, Trace can be implemented through specific pages and on the Web. in the Config configuration file, define the implementation. In this way, after the program debugging is completed, set Trace to Off. In this way, the program will not have the debugging function.
1) Implementation of page debugging
When debugging is required for a specific page, we can set it as follows:
- ﹤%@ Page Language="VB" Trace="True" %﹥
2) define WEB. Config implementation
In WEB. CONFIG, we can also enable program debugging:
- ﹤configuration﹥
- ﹤system.web﹥
- ﹤trace enabled="true" requestLimit="10" localOnly="false"/﹥
- ﹤/system.web﹥
- ﹤/configuration﹥
After you use the preceding settings to open Trace, you can use Trace in a specific program to debug the program. For example:
- Trace.Write("This is some custom debugging information")
Or debug program variables:
- Trace.Write("This is is my variable and it's value is:" & myVariable.ToString())
The above settings show that in ASP. NET, the program debugging function is very convenient and simple.
Some of the habits and notes of ASP. NET programming are introduced here. I hope it will help you.
- Nine steps for learning ASP. NET
- Advantages of ASP. NET compared with ASP
- Analysis of ASP. NET MD5 and SHA1 encryption methods
- ASP. NET learning-CSS implementation multi-interface two methods
- Basic Analysis of Cookie programming in ASP. NET