Asp. NET programming habits

Source: Internet
Author: User
Tags config error handling insert integer tostring
Asp.net| programming Novice programming friends often like to collect some very "wonderful" programming skills, however, the accumulation of skills often did not improve the quality of the program, but to guide some programmers blindly pursuit of strange and new, forget the basic programming habits of the training, not conducive to team cooperation, May, This is one of the reasons why there is no shortage of smart programmers in China, but a lack of smart development teams. In the development of ASP.net, we can learn a lot of skills, but some basic programming habits we must develop, so that not only can fundamentally improve the quality of the program and development efficiency, but also conducive to the reading of the program and team development. If you write the program only you can read or only a few people can understand, even if the program skills gadget its technology, for the program upgrades and maintenance are fatal problems.

I. Processing of errors (other than)
The most basic requirement of program robustness is the process and capture of program error, in asp.net, the error processing has the same mechanism as other programming languages, you can use try ... Catch... Finally and so on, this is compared with the ASP has a greater progress. Moreover, using these error-handling methods can greatly improve the readability of the program and the speed of the program debugging, in the combination of these advantages, we should pay more attention to this point.
For error handling, we can refer to this article (in English):
http://www.123aspx.com/redir.aspx?res=28336

Second, the processing of strings
In web design, the processing of strings is almost the most common. With ASP.net, strings are processed faster than ASPs, and in ASP.net, a string-handling class Stringbulider is used to perform some common string operations, and the most important, using StringBuilder can greatly increase string processing speed.
In ASP.net, the most common use of "&" is 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 look at the use of StringBuilder, when using StringBuilder, we can do some basic operations on strings, such as Append, Replace, Insert, remove, and so on, now we look at specific examples.
(1) The use of append in StringBuilder
Append, like the Append in other languages, is the addition of 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, using the Append method to achieve a table output, it should be noted that Stringbulider must first use the ToString () method to convert it to a string type can be directly output. In the above example, all we see is append a direct string, in fact, this method has a very convenient function, that is, you can directly append other types of variables, such as directly appemd an integer type of value, of course, Our output is automatically converted to a string later:
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 ' > can append other types:</font>")
Sb.append (Varother)
Response.Write (Sb.tostring ())
End Sub
(2) Use of other methods in the string
We can also use other methods, we take a look at the common:
Insert method, you can insert other characters at the specified location, using the method: Insert (insert position, insert character);
Remove method, you can delete the specified number of characters at the specified location, using the method: remove (In fact, the number of characters);
Replace method, which replaces the specified character, using the method: replace (substituted string, replacement string)
You can refer to the following articles for specific descriptions and usage of strings:
Http://aspfree.com/aspnet/stringbuilder.aspx
Http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemTextStringBuilderClassTopic.asp

Third, the database link connection and DataReader close
When using ASP programming, we already know that after using a database connection, be sure to close the connection and set it to nothing. In asp.net, we still need to use this, but in ASP.net, because of the use of ado.net, so in some related processing, there are actually some subtle differences, and these differences, often is the design of the most need to pay attention to. Now, for example, let's look at some of the issues that need to be noted in common ado.net operations.
(1) For example a
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 are nothing Then
' Close DataReader
Mydatareader.close ()
End If
End Try
In the above example, we note that this only closes the DataReader and does not close the connection. Why, then? Careful observation of the above ExecuteReader method, the original, set the ExecuteReader parameters, when the completion of ExecuteReader, will automatically close the connection. So, after this setting, there is no need to manually shut down the connection.
(2) Example two
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 are nothing AndAlso (myconnection.state and ConnectionState.Open) = ConnectionState.Open) Then
Myconnection.close ()
End If
End Try
In the above example, we found that actually did not close the DataReader. Why, then? In fact, the above code, there is no direct generation of DataReader objects, of course, can not be closed. It is important to note that before closing the connection, the program first determines whether the connection has been turned on, and if not, there is no need to close it.

Four, use Web.config/maching.config to save the commonly used data
Some of the data we need to use often, such as the use of ado.net, the most common is the database connection statements, in the ASP, we often keep this information in the application. Of course, in asp.net, you can do this, however, ASP.net has provided a configuration file web.config, so



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.