Solved

How to catch SQL Exception in ASP?

Posted on 2001-07-17
4
235 Views
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!
0
Comment
Question by:john_law70
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
weesiong earned 16 total points
ID: 6289213
john_law70,

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

Regards,
WEe Siong
0
 

Expert Comment

by:holymud
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
0
 
LVL 7

Expert Comment

by:weesiong
ID: 6289221
join_law70,

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

Regards,
Wee Siong
0
 
LVL 1

Expert Comment

by:deestuar
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 ?
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

13 Experts available now in Live!

Get 1:1 Help Now