Analysis of ASP. NET Programming habits

Source: Internet
Author: User
Tags dsn sha1 encryption

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:

 
 
  1. Dim myOutputString As String = "My name is"   
  2. Dim myInputString As String = " Alex"   
  3. myOutputString = myOutputString & myInputString   
  4. 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.

 
 
  1. Dim sb as StringBuilder = New StringBuilder()   
  2. sb.append( "﹤table border='1' width='80%'﹥" )   
  3. For i = 0 To﹤ RowCount - 1   
  4. sb.Append("tr﹥")   
  5. For k = 0 To ColCount - 1   
  6. sb.Append("﹤td﹥")   
  7. sb.Append( dt.Rows(i).Item(k, DataRowVersion.Current).toString())   
  8. sb.Append( "﹤/td﹥" )   
  9. Next   
  10. sb.Append("﹤tr﹥")   
  11. Next   
  12. sb.Append( "﹤/table﹥")   
  13. Dim strOutput as String = sb.ToString()   
  14. 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:

 
 
  1. Sub Page_Load (Source As object, E As EventArgs)
  2. Dim sb As System. Text. StringBuilder
  3. Dim varother As Integer
  4. Varothers = 9999
  5. Sb =NewSystem. Text. StringBuilder ()
  6. Sb. append ("<Font color = 'blue'> Append other types: </font>")
  7. Sb. append (varother)
  8. Response. write (sb. toString ())
  9. 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

 
 
  1. Dim myConnection As SqlConnection =NewSqlConnection (ConfigurationSettings. receivettings ("DSN_pubs"))
  2. Dim myCommand As SqlCommand =NewSqlCommand ("Select pub_id, pub_name From publishers", MyConnection)
  3. Dim myDataReader As SqlDataReader
  4. Try
  5. MyConnection. Open ()
  6. MyDataReader = myCommand. ExecuteReader (CommandBehavior. CloseConnection)
  7. DropDownList1.DataSource = myDataReader
  8. DropDownList1.DataBind ()
  9. Catch myException As Exception
  10. Response. Write ("An error has occurred :"& MyException. ToString ())
  11. Finally
  12. If Not myDataReader Is Nothing Then
  13. 'Close DataReader
  14. MyDataReader. Close ()
  15. End If
  16. 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

 
 
  1. Dim myConnection As SqlConnection = new SqlConnection(ConfigurationSettings.AppSettings("DSN_pubs"))   
  2. Dim myCommand As SqlCommand = new SqlCommand("Select pub_id, pub_name From publishers", myConnection)   
  3. Try   
  4. myConnection.Open()   
  5. dropDownList1.DataSource = myCommand.ExecuteReader()   
  6. dropDownList1.DataBind()   
  7. Catch myException As Exception   
  8. Response.Write("An error has occurred: " & myException.ToString())   
  9. Finally   
  10. If Not myConnection Is Nothing AndAlso ((myConnection.State And ConnectionState.Open) = ConnectionState.Open) Then   
  11. myConnection.Close()   
  12. End If   
  13. 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:

 
 
  1. ﹤configuration﹥   
  2. ﹤appsettings﹥   
  3. ﹤add key="dsn" value="myserver"/﹥   
  4. ﹤add key="someotherkey" value="somevalue"/﹥   
  5. ﹤/appsettings﹥   
  6. ﹤/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:

 
 
  1. ﹤html﹥   
  2. ﹤script language="VB" runat=server﹥   
  3. Sub Page_Load(Sender as object, E as EventArgs)   
  4. Dim AppSettings as Hashtable = Context.GetConfig("appsettings")   
  5. DSN.Text = AppSettings("dsn")   
  6. SomeOther.Text = AppSettings("someotherkey")   
  7. End Sub   
  8. ﹤/script﹥   
  9. ﹤body﹥   
  10. DSN Setting: ﹤asp:label id="DSN" runat=server/﹥ ﹤br﹥   
  11. Some Other Setting: ﹤asp:label id="SomeOther" runat=server/﹥   
  12. ﹤/body﹥   
  13. ﹤/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:

 
 
  1. ﹤%@ Page Language="VB" Trace="True" %﹥  

2) define WEB. Config implementation

In WEB. CONFIG, we can also enable program debugging:

 
 
  1. ﹤configuration﹥   
  2. ﹤system.web﹥   
  3. ﹤trace enabled="true" requestLimit="10" localOnly="false"/﹥   
  4. ﹤/system.web﹥   
  5. ﹤/configuration﹥ 

After you use the preceding settings to open Trace, you can use Trace in a specific program to debug the program. For example:

 
 
  1. Trace.Write("This is some custom debugging information")  

Or debug program variables:

 
 
  1. 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.

  1. Nine steps for learning ASP. NET
  2. Advantages of ASP. NET compared with ASP
  3. Analysis of ASP. NET MD5 and SHA1 encryption methods
  4. ASP. NET learning-CSS implementation multi-interface two methods
  5. Basic Analysis of Cookie programming in ASP. NET

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.