error handling - rolling back changes

error handling - rolling back changes

am 24.04.2007 06:49:42 von 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

Re: error handling - rolling back changes

am 24.04.2007 11:49:05 von Anthony Jones

wrote in message
news:1177390181.929509.98340@c18g2000prb.googlegroups.com...
> 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

Re: error handling - rolling back changes

am 24.04.2007 22:47:37 von 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 & "

"& myError.Number & " - " & myError.Description &
"

"
ErrorsFound = True
End If
Next
ElseIf err.number <> 0 then
response.Write(Err.Description&"

")
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...
"
Else
conn.RollbackTrans
Response.Write "Rolling back transaction...
"
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

Re: error handling - rolling back changes

am 25.04.2007 00:19:20 von reb01501

brendan.wong@gmail.com wrote:
> 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 & "

"& myError.Number & " - " & myError.Description &
> "

"
> ErrorsFound = True
> End If
> Next
> ElseIf err.number <> 0 then
> response.Write(Err.Description&"

")
> 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...
"
> Else
> conn.RollbackTrans
> Response.Write "Rolling back transaction...
"
> 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.

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: error handling - rolling back changes

am 25.04.2007 03:25:10 von 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 & "

"& myError.Number & " - " & myError.Description &
"

"
ErrorsFound = True
End If
Next
ElseIf err.number <> 0 then
response.Write(Err.Description&"

")
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...
"
Else
conn.RollbackTrans
Response.Write "Rolling back transaction...
"
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