Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

How to catch SQL Exception in ASP?

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!
  • 2
1 Solution

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

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
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 ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now