error handling-duplicate key

M

monika

hi...

when I try to insert a duplicate record I get the primary key error. which
is good. but I want to generate a user friendly error ... telling the user
that u have got this error because u r trying to insert a duplicate record.
i can simply do so by using "on error resume next"....
this is what I do:
"on error resume next
server.transfer("error.asp")"
the problem is it simply takes me to a page where I display an error has
occurred... it doesn't look attractive to display a one line error in a big
page? I hope I am clear....
1. how can I display the error message in my error.asp
2. I can I make it look better... how is error handling done ???
I am a newbie and would appreciate if anyone can give me some examples,.
thanks

<%
on error resume next
server.transfer("error.asp")
%>
<html>
<head>
<title>sdad</title>
</head>
<body>
<table><tr><td bgcolor="SILVER">
<BIG> <B> <FONT COLOR="#000080"> <%=session("TxtName")%> your story is
submitted!!!&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Today is <%=DATE()%></b></td><p></p></tr>
</table><p></p>

<%
Dim strStoryContent
DIM RSA
DIM QUERY1
DIM Conn
dim adCmdText
dim currentdate

'for debug
'For Each Item in Request.Form
'Response.Write(Item & " = " & Request.Form(Item) & "<br>")
'Next

adCmdText = 1
'create and open database connection
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "dsn=school"

'create commad object
set objCmd = server.createobject("adodb.command")

currentDate = date()
strStoryContent = Request.form("mytextarea")

If Request.form("submit") <> "" then
If Request("mytextarea")<>"" then
Set RSA = Server.CreateObject("ADODB.Recordset")
QUERY1 = "INSERT INTO student_content
(student_id,story_id,status_student,story_original,story_status_date,story_w
ord_count) VALUES
("&session("loggedID")&","&session("selectStoryID")&",'"&Request.form("Submi
t")&"','"&strStoryContent&"','"&currentDate&"',"&request.form("wordsCount")&
")"
set objCmd.ActiveConnection = Conn
objCmd.CommandText = Query1
objCmd.CommandType = adCmdText
'execute the command
objCmd.Execute
else
response.write "You have not written the story?"
End If
end if


'close and dereference database objects
set objCmd = nothing
conn.close
set conn = nothing
%>
</body>
</html>
 
R

Ray at

I would modify your code like this.

<%
Dim strStoryContent
DIM RSA
DIM QUERY1
DIM Conn
dim adCmdText
dim currentdate
Dim sSQL

currentDate = date()
strStoryContent = Request.form("mytextarea")

If Request.form("submit") <> "" then
If Request("mytextarea")<>"" then


'create and open database connection (if needed)
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "A connection string from www.connectionstrings.com"

sSQL = "SELECT story_id FROM student_content WHERE story_id=" &
Session("selectStoryID")
Set rsTest = Conn.Execute(sSQL)
If rsTest.EOF Then
'''story ID doesn't exist, continue on

rsTest.Close
Set rsTest = Nothing
Conn.Close
Set Conn = Nothing

QUERY1 = "INSERT INTO student_content
(student_id,story_id,status_student,story_original,story_status_date,story_w
ord_count) VALUES
("&session("loggedID")&","&session("selectStoryID")&",'"&Request.form("Submi
t")&"','"&strStoryContent&"','"&currentDate&"',"&request.form("wordsCount")&
")"

conn.Execute QUERY1
Else
rsTest.Close
Set rsTest = Nothing
Conn.Close
Set Conn = Nothing
Response.Write "That story ID already exists."

else
response.write "You have not written the story?"
End If
end if
%>

In addition to those modifications, I would also uses spaces when
concatenating. For a sample reason as to why, try this:
<%
Dim h1, h2
h1 = "Floyd"
h2 = "Isabel"

Response.Write h1&"wasn't bad. Hopefully"&h2&" won't bust up my trees."
%>

Ray at work
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top