How to catch SQL Exception in ASP?

Posted on 2001-07-17
Last Modified: 2008-02-01
I have make a SQL Statement to insert into db:

strSQL = "insert into lgr_logreg values(" + strUI + ",'" + strPassword + "','" + strEmail + "','" &_
 strTel + "','" + strRank + "','" + strPosition + "','" + strFullname +"','" + strRemoteIP + "','2001-07-01','test');"

This is true that sometimes the user will insert duplicated Primary Key Value. So, how can catch the SQL exception of ASP or can i test the statement to return boolean for me ? like

trueOrFalse = connection.Execute(strSQl)

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14)
[Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_lgr_logreg'. Cannot insert duplicate key in object 'lgr_logreg'.
/regpoint.asp, line 33

Please give me some code example. Thanks!
Question by:john_law70
  • 2

Accepted Solution

weesiong earned 16 total points
ID: 6289213

StrSQL = "Select strUI from lgr_logreg where strUI =" +strUI StrSQL, conn, 1, 3
IF Not Rs.Eof Then
   Response.Write("Sorry canot using this userid")
strSQL = "insert into lgr_logreg values(" + strUI + ",'" + strPassword + "','" + strEmail + "','" &_
strTel + "','" + strRank + "','" + strPosition + "','" + strFullname +"','" + strRemoteIP + "','2001-07-01','test');"
End IF

WEe Siong

Expert Comment

ID: 6289219
on error resume next

Execute (sql)

if err <> 0 then    ' an error has occurred
' now u have the error number and descriotion

resposne.write err.number
resposne.write err.description

err.clear     'dont forget to clear the error
end if

Expert Comment

ID: 6289221

Questions Asked 6
Last 10 Grades Given A D  
Question Grading Record 2 Answers Graded / 2 Answers Received

5 11/16/00 100 VB-How to save binary data to a file? UnlockedQ

Please close Open Question

Wee Siong

Expert Comment

ID: 6289316
Howe about putting in some transaction management in the database and use raiserror when an exception occurs. You could code your insert statment into a server side stored procedure ?

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: and…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now