locked
Get unique error code for a SMO failure call. RRS feed

  • Question

  • I got this error when doing log backup on SQL 2012 AG secondary database using SMO:

    System.Data.SqlClient.SqlError: Log backup for database "db2" on a secondary replica failed because the last backup LSN (0x00000024:00000103:0001) from the primary database is greater than the current local redo LSN (0x00000024:000000fd:0001). No log records need to be backed up at this time. Retry the log-backup operation later.

    What I want to do is to handle this specific error in my code. I am not sure I can distinguish this error from other backup errors using SMOException. I can see there is an event as Error: 3041, Severity: 16, State: 1 in SQL log, but I believe this is same as other backup failures even if I can get this 3041 error number from SMOException. I am not sure I can use HResult in SMOException, as it is protected member variable. I also don’t think I can use string comparsion with the error message, as I would get different message with different lauguage.

    In summary, I would like to get an unique error code, or an unique exception for this specific error using SMO.

    By the way, with DMO, I got this:

    Source: Microsoft SQL-DMO (ODBC SQLState: 42000)

    Error Message: IDispatch error #34783

    Description: Log backup for database "test1_ag2" on a secondary replica failed because the last backup LSN (0x00000020:000000ad:0001) from the primary database is greater than the current local redo LSN (0x00000020:000000ac:0001). No log records need to be backed up at this time. Retry the log-backup operation later.

    BACKUP LOG is terminating abnormally.

    Monday, June 4, 2012 8:33 PM

Answers

All replies

  • Hi qzhang1,

    You may try to use Try-Catch method to capture exceptions in SMO. For inner exceptions, which may be caused by an SQL exception, leads to a failure SMO, please pay attention to InnerException to determine the original error.  For more information: Handling SMO Exceptions.

    TechNet Subscriber Support
    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Stephanie Lv

    TechNet Community Support

    Tuesday, June 5, 2012 2:40 AM
  • Stephanie,

    I already use Try-Catch method as I mentioned, the above error message is from InnerException of SMOException. What I am looking for is to get the unique error code for the above error, so I can check to see if this particular error has happened or not without the need to parse the error message. Something like this:

    try{ SMObackup

    }catch(SmoException smoex){

    if(HResult == 0x80003401) doing something;}

    Thanks.

     

    Tuesday, June 5, 2012 3:41 AM
  • Hello qzhang1,

    I suppose that Stephanie was speaking of SmoException.GetBaseException() when she wrote "pay attention to InnerException to determine the original error"

    see http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.smoexception(SQL.105).aspx

    the message for the original error can be get thru smoex.GetBaseException().Message

    the type of the original error can be get thru smoex.GetBaseException().GetType().FullName.

    You can also use the StackTrace property of your smoex. Less evident to use but giving informations which could be useful.

    Please, could you explain your code line ?

    if(HResult == 0x80003401) doing something;

    why and how are you testing HResult ? ( i was thinkung that the HResult was a protected property not available from the SMO classes ( at least in VB and VC# ). Please, could you tell us what is the explanation for the value 0x80003401 ? Since the release of SQL Server 2005, i am using SMO and i never used this value.

    We are waiting for your feedback to try to help you more efficiently.

    Have a nice day 


    Mark Post as helpful if it provides any help.Otherwise,leave it as it is.

    Saturday, June 9, 2012 10:26 PM
  • Hi Papy,

    As I mentioned, I want to detect if this specific error "Log backup for database.., Retry the log-backup operation later" happened or not in my code. 0x8003401 is just an error code I made up to show what I want to do. I want to get an unique error code for a SMO call failure, so I can test this error code in my code to do something accordingly.

    If you have used Win32 API, you know most of APIs would return an error code, or you can get the code by calling getlasterror(). For example, calling createfile(), if the call returns file exists error, your code can act accordingly.

    I understand HResult is protected member, I used it to show what I want to do, as I am not sure what you suggested such as get exception type would help in my case, as they are not unique.

    Thanks!

    Sunday, June 10, 2012 12:04 AM
  • Hello,

    The problem is that SMO is calling ( usually ) classes from System.Data.SqlClient like SqlConnection or SqlCommand.So , in case of error/exception , the GetBaseException() would be a SqlException and you can get the value of HResult from the properties of the SqlException

    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlexception_members(VS.90).aspx

    This property is ErrorCode

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.externalexception.errorcode(VS.90).aspx

    When i am writing a SMO program, i am surrounding ( like you ) every part with a try/catch block , more exactly i am using 3 catch :

    - the 1st one is for a SqlException and i display the values of Class ,  ErrorCode , LineNumber ,  Message , Number , Procedure , Server , Source , State and TargetSite.Name

    - the 2nd one is for a SmoException and i display the values of Message , SmoExceptionType , Source and TargetSite.Name

    - the 3rd one is for a (generic) Exception and i display the values of Message , Source , TargetSite.Name

    It is when i have discovered this article

    http://blogs.msdn.com/b/jason_howell/archive/2012/06/11/analysis-services-cube-processing-fails-with-error-quot-ole-db-error-ole-db-or-odbc-error-operation-canceled-hy008-quot.aspx 

    that i decided to post again to suggest you this way of coding ( usually i found my error in less than 5 minutes ).

    I hope that it could help you.

    Don't hesitate to post again for more help or explainations.

    Have a nice day

    PS : if i have a good remembrance, you could retrieve the explanation of an ErrorCode value in the http://msdn.microsoft.com/en-us/library/ms165761(SQL.105).aspx


    Mark Post as helpful if it provides any help.Otherwise,leave it as it is.

    • Marked as answer by Iric Wen Friday, June 15, 2012 8:04 AM
    Tuesday, June 12, 2012 10:47 AM