Error in applying database uniqueness constraints and capturing databases in ASP

Source: Internet
Author: User
Tags driver manager error code implement insert odbc numeric value
Error | data | Database The ASP pages mentioned in this article refer to < @PAGE languege= "VBSCRIPT" except for special notices >
When writing a program that combines ASP with a database, you often encounter problems where duplicate values are not allowed in the same datasheet, such as not allowing the same user ID to appear in the user registration program. In this case, I used to read the existing value in the database and then compare it to the value entered by the user, and if so, the prompt has been registered and must be filled in again. This can be done for the intended purpose, but it is a waste of resources because of an additional database access operation.
As we all know, the columns in the datasheet can set a uniqueness constraint, that is, to restrict the field from the database to duplicate values, the database will return a 2627 error after violating the uniqueness constraint, prompting "violation of the%1! Bound '%2! '. You cannot insert a duplicate key in the object '%4! ', which is the output of the database, and all errors in SQL Server are stored in the system table master.dbo.sysmessages. User-defined messages can also be stored in sysmessages. When a stored procedure is invoked in an ASP, because the stored procedure does not have fault-tolerant processing, it returns the prompt to the page and terminates the execution of the statement, @ @Error returns a value of 0. If the page has fault tolerance, the error message does not appear, but because the error is present in the database, Err.Number cannot get its error number. (also may be my database is not familiar with the reason:-), also please shrimp pointing). Recently studied ADO, found a more convenient way to implement the control.
ADO (ActiveX Data Objects) is widely used in the application and database connection, ASP can also through him to implement the operation of the database. The connection object in ADO 2.1 has an Error object that allows you to capture errors from the database for control in the ASP page. The object's commonly used property has the following, read-only:
Description: Character type, returning error description from database
Source: Character type, error source
SQLState: Character type, ODBC API (Driver Manager) error, as described in SQL Server 2000:
The string value returned by SQLSTATE consists of a two-character class value followed by a three-character subclass value. A class value of 01 indicates a warning followed by the Sql_success_with_info return code. As long as the class value is not (except for IM), it is an error, followed by the Sql_error return code. Class IM is specifically used to indicate warnings and errors that are generated from an ODBC implementation. The neutron class value 000 of any class is used to implement the conditions defined in the given class. The allocation method for this class value and subclass value is defined by SQL-92.
Digress to digress, back to just the topic ~ ~ ~
Number: Returns the numeric value associated with the error, which is a 32-bit value. The higher 16-bit word is the facility code, and the lower word is the real error code.
NativeError: That's what we're going to use today, and MSDN describes him as:
The NativeError property on a Error (is not a m$ bug?) My English is less than:-)) object indicates the provider-specific error code for a given Error object. This property returns a Long value that indicates the error code.
There are HelpFile and helpcontext, all related to help, here is not detailed.
Here we used the NativeError to catch the error from the database, and he returned the wrong number. The source program fragment is as follows:
Fault tolerance for <%on ERROR RESUME NEXT ' VBScript
Set Conn = Server.CreateObject ("ADODB. Connection ")
Set RS = Server.CreateObject ("ADODB.") RecordSet ")

Conn.Open "DSN=BBII; Uid=sa; Pwd= "
Sql= "Exec ch_insnovel ' d ', ' 22 ', ' 33 ', ' 44 ', ' 55 '"
' SQL = ' Insert into novel (Name,author,yy,ee) VALUES (' nn ', ' ee ', ' JJ ') ", as the previous sentence effect is the same
Rs. Open Sql,conn
If Conn.Errors.Count = 0 Then ' To determine the number of errors
ISSUCC = "Success"
Else
Select Case Conn.Errors.Item (0). NativeError
Case 2627
ISSUCC = "DDDDDDD" You can define multiple own error return values as needed
End Select
End If
Response.Write "<br&gt:::" &AMP;ISSUCC
%>
Note Select Case ... "Conn.Errors.Item (0)" in the sentence. NativeError, returns the first of a set of item. In our case, along with the 2627 measures that occur at the same time is "statement terminated" (3621), strictly speaking, he is not an error, but a general warning (severity level =10). So we need to judge the first error, of course, you can define i = i + to see all the errors that occur. This allows us to know the exact contents of the error from SQL Server and make it easier to control.

Attached: Error number
<!-ErrLp.asp-->
<%on ERROR RESUME NEXT
Set Conn = Server.CreateObject ("ADODB. Connection ")
Conn.Open "Driver={sql Server}; server=127.0.0.1; Database=master; Uid=sa; Pwd= "
Set rs = Server.CreateObject ("ADODB.") RecordSet ")
SQL = "SELECT * from sysmessages Where Msglangid = 2052 ORDER by error"
Rs.Open sql,conn,3,2
PAGE = CLng (REQUEST ("Txtpage")
Rs. PageSize = 100
If Page < 1 Then page = 1
If PAGE > RS. PageCount Then PAGE = RS. PageCount
Rs. AbsolutePage = PAGE
%>
<style>
Td
{
Font-family:verdana;
font-size:10pt;
}
</style>
<Div>
<form method= "Get" >
<table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" bgcolor= "#999999" >
<TR>
&LT;TD width= > Total number: <%=rs. Recordcount%></td>
&LT;TD width= > Total pages: <%=rs. Pagecount%></td>
&LT;TD width= ">" Current page:<%=page%></td>
&LT;TD width= "" "> Go to <input type=" text "name=" Txtpage "size=" 2 "style=" Font-family:verdana; font-size:8pt; Border-style:solid; Border-width:1 "> Page </TD>



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.