Exceptions

Posts   
 
    
ippy04
User
Posts: 1
Joined: 20-Feb-2004
# Posted on: 20-Feb-2004 03:50:37   

Hi, I've been playing around with LLBLGen and I've found it to be pretty impressive so far. I do have a question tho - Is it possible to figure out what kind of SqlError is thrown when the database returns an exception?

i.e. if I try to insert a duplicate value into a primary key value I get this exception:

Exception Details: SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of an action query: Violation of PRIMARY KEY constraint 'PK_Users'. Cannot insert duplicate key in object 'UserDetail'. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

With a SQLException I can get the SqlError.number property to figure out what kind of error SQL server is returning. Do the generated classes have the same functionality?

Thanks!

tvoss avatar
tvoss
User
Posts: 192
Joined: 07-Dec-2003
# Posted on: 20-Feb-2004 04:59:46   

If you examine the innerexception you will find the sqlexception there.

Do you know where I can get a listing of sqlerror.number types with explanation? I just found sysmessages in master db.

And further if you know: How would one find all the errors that could relate to a database being down.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 20-Feb-2004 09:21:20   

As Terry says, (and the exception also wink ) the inner exception of that exception caught is an SqlException. Cast it to an SqlException (or OracleException, if you're on Oracle) and use its properties to determine the error number.

You can find a complete list of errors in Books Online of SqlServer. TroubleShooting -> Error Messages -> Error Message Descriptions simple_smile

Frans Bouma | Lead developer LLBLGen Pro
wayne avatar
wayne
User
Posts: 611
Joined: 07-Apr-2004
# Posted on: 15-Apr-2004 13:53:07   

Hi All

I am receiving a 'Violation of UNIQUE KEY constraint'. I atually want the SQL error number.

So i tried to cast the exception to a SQLException - but that gives me 'Specified cast is not valid' then i tried casting the Inner Exception as explained in this message thread - But that returns '<undefined value>'disappointed

What else can i try? confused

Code snippet of first try

public bool Save()
{
     try  
    {   
           return _Agency_Entity.Save();
    }
    catch (Exception ex)
    {
           SqlException sex;
           sex = (SqlException)ex; //<--- 'Specified cast is not valid' 
           //Do something...
    }
}

Code snippet of 2nd try

public bool Save()
{
     try  
    {   
           return _Agency_Entity.Save();
    }
    catch (Exception ex)
    {
           SqlException sex;
           sex = (SqlException)ex.InnerException; //<--- '<undefined value>'[/color]
           //Do something...
    }
}

Error Message caught {"An exception was caught during the execution of an action query: Violation of UNIQUE KEY constraint 'AgencyName'. Cannot insert duplicate key in object 'AgencyRecord'.\r\nThe statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception." }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 15-Apr-2004 14:34:03   

You should cast the ex you catch to an ORMQueryExecutionException exception or better:

try { // your code } catch(ORMQueryExecutionException ex) { // in here, check the ex.InnerException }

Frans Bouma | Lead developer LLBLGen Pro