error handling - rolling back changes

B

brendan.wong

hello. i'm trying to incorporate error handling into my application,
but i've run into a dilemma.

i've already performed 10 successful INSERTS, but on the 11th INSERT,
the application fails for some reason (say for example, i tried to
perform an INSERT into a table that doesn't exist). logically, i
should stop execution and display some sort of error message. but,
i've already ran a bunch of INSERTS so what do i do? thanks
 
A

Anthony Jones

hello. i'm trying to incorporate error handling into my application,
but i've run into a dilemma.

i've already performed 10 successful INSERTS, but on the 11th INSERT,
the application fails for some reason (say for example, i tried to
perform an INSERT into a table that doesn't exist). logically, i
should stop execution and display some sort of error message. but,
i've already ran a bunch of INSERTS so what do i do? thanks

Look up your DB platform's help documentation on:-

BEGIN TRANSACTION
COMMIT TRANSACTION
ROLLBACK TRANSACTION
 
B

brendan.wong

thanks. that helps out alot.

i have another question. i have the following test code:

'-------------------------------beg of
code------------------------------------------------

Function ErrorsFound(mycon) '-----------------beg of function
Dim myError

If mycon.State <> 1 Then
eStr = "something wrong with db"
ErrorsFound = True
ElseIf mycon.Errors.Count > 0 Then
For Each myError in mycon.Errors
If myError.Number <> 0 Then
eStr = eStr & "<P>"& myError.Number & " - " & myError.Description &
"</P>"
ErrorsFound = True
End If
Next
ElseIf err.number <> 0 then
response.Write(Err.Description&"<br><br>")
ErrorsFound = True
Else
ErrorsFound = False
End If
End Function '-----------------end of function


query1 = "insert into players (lastname, firstname, rbi) values
('cruise','beta',44)"
query2 = "insert into players (lastname, firstname, rbi) values
('jee','alison',57)"
query3 = "insert into players (lastname, firstname, rbi) values ('van
hudgeons','vanessa',123)"
query4 = "insert into players (lastname, firstname, rbi) values
('fox','megan',99)"

conn.BeginTrans
conn.execute query1
conn.execute query2
conn.execute query3
conn.execute query4
If ErrorsFound(conn) = False Then
conn.CommitTrans
Response.Write "Committing Transaction...<br>"
Else
conn.RollbackTrans
Response.Write "Rolling back transaction...<br>"
End If
'-------------------------------end of
code------------------------------------------------

for kicks, i changed the name of the table to force an error. i
changed it for the first query, then the second, then the third, and
finally the last. the thing i'm wondering is when i changed the
first, second, and third queries, the ErrorsFound function will enter
the 2nd "elseif," but for the fourth query, the function will enter
the 1st "elseif." why is that?

thanks
 
B

Bob Barrows [MVP]

thanks. that helps out alot.

i have another question. i have the following test code:

'-------------------------------beg of
code------------------------------------------------

Function ErrorsFound(mycon) '-----------------beg of function
Dim myError

If mycon.State <> 1 Then
eStr = "something wrong with db"
ErrorsFound = True
ElseIf mycon.Errors.Count > 0 Then
For Each myError in mycon.Errors
If myError.Number <> 0 Then
eStr = eStr & "<P>"& myError.Number & " - " & myError.Description &
"</P>"
ErrorsFound = True
End If
Next
ElseIf err.number <> 0 then
response.Write(Err.Description&"<br><br>")
ErrorsFound = True
Else
ErrorsFound = False
End If
End Function '-----------------end of function


query1 = "insert into players (lastname, firstname, rbi) values
('cruise','beta',44)"
query2 = "insert into players (lastname, firstname, rbi) values
('jee','alison',57)"
query3 = "insert into players (lastname, firstname, rbi) values ('van
hudgeons','vanessa',123)"
query4 = "insert into players (lastname, firstname, rbi) values
('fox','megan',99)"

conn.BeginTrans
conn.execute query1
conn.execute query2
conn.execute query3
conn.execute query4
If ErrorsFound(conn) = False Then
conn.CommitTrans
Response.Write "Committing Transaction...<br>"
Else
conn.RollbackTrans
Response.Write "Rolling back transaction...<br>"
End If
'-------------------------------end of
code------------------------------------------------

for kicks, i changed the name of the table to force an error. i
changed it for the first query, then the second, then the third, and
finally the last. the thing i'm wondering is when i changed the
first, second, and third queries, the ErrorsFound function will enter
the 2nd "elseif," but for the fourth query, the function will enter
the 1st "elseif." why is that?
You need to check for errors after each execution, rolling back if errors
occur. Successful executions clear the errors.
 
B

brendan.wong

thanks. that helps out alot.

i have another question. i have the following test code:

'-------------------------------beg of
code------------------------------------------------

Function ErrorsFound(mycon) '-----------------beg of function
Dim myError

If mycon.State <> 1 Then
eStr = "something wrong with db"
ErrorsFound = True
ElseIf mycon.Errors.Count > 0 Then
For Each myError in mycon.Errors
If myError.Number <> 0 Then
eStr = eStr & "<P>"& myError.Number & " - " & myError.Description &
"</P>"
ErrorsFound = True
End If
Next
ElseIf err.number <> 0 then
response.Write(Err.Description&"<br><br>")
ErrorsFound = True
Else
ErrorsFound = False
End If
End Function '-----------------end of function


query1 = "insert into players (lastname, firstname, rbi) values
('cruise','beta',44)"
query2 = "insert into players (lastname, firstname, rbi) values
('jee','alison',57)"
query3 = "insert into players (lastname, firstname, rbi) values ('van
hudgeons','vanessa',123)"
query4 = "insert into players (lastname, firstname, rbi) values
('fox','megan',99)"

conn.BeginTrans
conn.execute query1
conn.execute query2
conn.execute query3
conn.execute query4
If ErrorsFound(conn) = False Then
conn.CommitTrans
Response.Write "Committing Transaction...<br>"
Else
conn.RollbackTrans
Response.Write "Rolling back transaction...<br>"
End If
'-------------------------------end of
code------------------------------------------------

for kicks, i changed the name of the table to force an error. i
changed it for the first query, then the second, then the third, and
finally the last. the thing i'm wondering is when i changed the
first, second, and third queries, the ErrorsFound function will enter
the 2nd "elseif," but for the fourth query, the function will enter
the 1st "elseif." what makes the fourth query any different than the
first 3 that it won't enter the same "if" branch? what's going on?

thanks
 

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

Similar Threads


Members online

Forum statistics

Threads
474,266
Messages
2,571,083
Members
48,773
Latest member
Kaybee

Latest Threads

Top