Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Error handling using ADO

Author  Topic 

cosku
Starting Member

4 Posts

Posted - 2002-01-10 : 05:29:24
Why can't I capture the raised error when I execute

conn.Execute("select n=1 into #t; raiserror('t',16,1)")

or

conn.Execute("insert into t values(1); raiserror('t',16,1)")
--where t is a table having a single integer column

or

conn.Execute("insert into t values(1); insert into t values('a')")
--again where t is a table having a single integer column

from an VB Application using ADO 2.7, SQL Server 2000 SP2


mfemenel
Professor Frink

1421 Posts

Posted - 2002-01-10 : 08:22:33
I believe you'd have to use ADO's error collection to return the error code back to your page.

Mike
"A program is a device used to convert data into error messages."
Go to Top of Page

cosku
Starting Member

4 Posts

Posted - 2002-01-10 : 08:32:31
conn.Errors.count
returns to be zero after the execution of sql statements
that's why I can't capture the error

that is to say I can capture the error if I write

conn.Execute("insert into t values('a')")

but cannot if i write

conn.Execute("insert into t values(1); insert into t values('a')")

thx

Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2002-01-10 : 14:16:07
I'm stuck on time so I can't try this out myself yet, but what are the results of the infomessages event of the connection object?



----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"
Go to Top of Page

cosku
Starting Member

4 Posts

Posted - 2002-01-11 : 07:50:09
infomessages event is not fired...

Go to Top of Page

Merkin
Funky Drop Bear Fearing SQL Dude!

4970 Posts

Posted - 2002-01-11 : 08:02:22
A few things here, they may or may not be totally relevant (I'm tired)

1. You will only be able to see errors for the last statement. So if you fire one that fails, but follow it up with a good one, you will not be able to see the error.

2. I belive the SQLOLEDB provider doesn't properly report all error descriptions properly.

3. To do what you want to do, I suggest wrapping the operation up in a stored proc with a transaction. Then you can guarantee that if one fails, it all will be rolled back.


Damian
Go to Top of Page

cosku
Starting Member

4 Posts

Posted - 2002-01-11 : 08:53:20
in fact my problem is reverse of first situation in my case first statement is a good one and the second fails but still i cannot capture the error

secondly even if the transaction is rolled back user will be unaware of the error occured which is a fatally undesirable situation

Go to Top of Page

mfemenel
Professor Frink

1421 Posts

Posted - 2002-01-11 : 09:29:43
What about returning a value of 1 or 0 back from your procedure. If it runs through normally return a 1 else 0. Might be a viable work around.

Mike
"A program is a device used to convert data into error messages."
Go to Top of Page

Lavos
Posting Yak Master

200 Posts

Posted - 2002-01-12 : 02:51:35
Unfortunately, I don't have the documentation with me so let me ignore my first response. I know most of the occassions when informessages gets called, I guess I misremembered errors being one of those times.

Anyway, I tried your examples, and they worked just fine for me.

For instance:

Private Sub Command1_Click()
con.Open
Debug.Print con.Errors.Count

con.Execute "select n=1 into #a; raiserror('t',16,1)"
Debug.Print con.Errors.Count

con.Close
End Sub

Produced exactly what it should've produced in the immediate window. I didn't have the exact same layout as you, (VB, tried with ADO 2.0, 2.5, and 2.6) with sql server 2000 (I don't know the service pack number off hand and can't check from here.) I tried it with your other examples, and I tried it with all three examples in sequence and checking con.Errors.Count between each call to Execute. It always reported 1 error after the statement executed.

It looks like there is either a bug in 2.7, or your code is wrong somehow. Are you checking Errors.Count immediately after the call to Execute, or at least before using any other ADO objects/methods?




----------------------
"O Theos mou! Echo ten labrida en te mou kephale!"
Go to Top of Page
   

- Advertisement -