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
 Development Tools
 ASP.NET
 How to retreive sqlexception from exception?

Author  Topic 

nomaneagle
Starting Member

18 Posts

Posted - 2008-11-11 : 09:51:59
I am working on an application which will write error log in Sql Server tables. I want to get the exception number so that I can take decision on the basis of exception number if exception will be written in database or not.

But the problem is that when I catch exception, it does not give me exception number. But when I catch SqlException then it gives me exception number. I am looking for one of the following solutions:

Try
Some code
Catch ex as exception
msgbox (ex.number)
end try

1. Is it possible to get ex.number in the above mentioned scenario?
2. Is it possible that I can convert exception into SqlException in order to get exception number?

Thanks for your help in advance.

Noman Siddiqui

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-11-11 : 09:55:58
[code]

catch (SqlException ex)
{
// handle SqlException
}
catch (Exception ex)
{
// handle other exceptions
}
[/code]


elsasoft.org
Go to Top of Page

nomaneagle
Starting Member

18 Posts

Posted - 2008-11-11 : 10:04:24
Thanks for the reply. Yes I am already aware of seperate catch for each exception. Normally exception has all other exceptions in it like SqlException and IOExceptions. So what Im trying to do is to filter my SqlException from Exception because exception conatins SqlException and other exceptions too. Thanks
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-11-11 : 10:19:01
you can do this but I don't see why my first suggestion doesn't work for you:

SqlException e = ex as SqlException;
if (e != null)
{
// do something with e
}


elsasoft.org
Go to Top of Page

nomaneagle
Starting Member

18 Posts

Posted - 2008-11-11 : 10:37:39
Thanks so much brother. It worked as you can see the below code.

Catch ex As Exception
Dim ea As SqlException
ea = CType(ex, SqlException)
If Not ea Is Nothing Then
MsgBox(ea.Number)
End If

End Try
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2008-11-11 : 14:08:14
quote:
Originally posted by nomaneagle

Thanks so much brother. It worked as you can see the below code.

Catch ex As Exception
Dim ea As SqlException
ea = CType(ex, SqlException)
If Not ea Is Nothing Then
MsgBox(ea.Number)
End If

End Try



This is vb.net, the above code is in c#, which one are u using ?

Do you know you can catch the error in MS SQL also ?
Go to Top of Page
   

- Advertisement -