none
Getting the DbException from a DbDataAdapter RRS feed

  • Question

  • Hello,

    I'm wondering how I can get the DbException thrown by operations the DbDataAdapter performs (if an error occurs). I couldn't find anything in the documentation.

    Thanks.

     

    Monday, April 25, 2011 9:34 PM

Answers

  • DbAdapter throws the different exceptions (ArgumentNull, InvalidOperation e.t.c.). Also anyone can implements the DbAdapter class and anyone can throws their exception types in overridden methods. If you want catch all exceptions with using same type; you should use the "System.Exception" type in "catch" statement.
    My suggestion is you should catch all exceptions, and throw again your own exception type, and catch its at one point.
    So, It's not ridiculous.

    Make everything as simple as possible, but not simpler.
    Tuesday, April 26, 2011 2:08 PM
  • If no DbException is thrown, you cannot retrieve it.
    Wednesday, April 27, 2011 7:57 AM
  •  

    Hi OLAPWisdom,

    Welcome!

    @Louis is right, we should catch this exception, Errors occurring at the data source are handled by the DbException code block, and all other exceptions are handled in the Exception block. DbDataAdapter use commands to communicate with database, we just can use catch method to retrieve the Excption.

    Inheritance Hierarchy

    --------------------------------------------------------------------------------

    System.Object

      System.Exception

        System.SystemException

          System.Runtime.InteropServices.ExternalException

            System.Data.Common.DbException

              System.Data.Odbc.OdbcException

              System.Data.OleDb.OleDbException

              System.Data.OracleClient.OracleException

              System.Data.SqlClient.SqlException

    You can refer here about how to handle data errors with DbException: http://msdn.microsoft.com/en-us/library/9hy8csk1.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, April 30, 2011 1:50 AM
    Moderator

All replies

  • Hello,

    Can you please take a look at my question in the following link and try to help:

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataset/thread/a5f66065-daec-4e85-816b-639cf4c17901

    Thanks.

    • Merged by Aspen VJ Wednesday, April 27, 2011 8:24 AM duplicated
    Monday, April 25, 2011 9:38 PM
  • You can do that like below:
    using (System.Data.Common.DbConnection cnn = new)
    {
      using (System.Data.Common.DbDataAdapter adapter = new)
      {
        DataTable dt = new DataTable();
    
        try{
        adapter.Fill(dt);
        adapter.Update(dt);
        }
        catch(Exception e){
          throw new DbException(e);
        }
      }
    }
    
    class CustomDbException : DbException
    {
      public CustomDbException(Exception e)
        : base("My DbException Message", e)
      {
      }
    }
    
    

    Make everything as simple as possible, but not simpler.
    Monday, April 25, 2011 10:00 PM
  • Excuse me, but that's a ridiculous "solution". I don't want to generate the DbException myself, I have to get the actual DbException that was thrown because I need to analyze it.

    Any other ideas, somebody?

     

    Tuesday, April 26, 2011 5:37 AM
  • Catch the DbException in a catch block:

    try
    {
         adapter.Fill(dt);
    }
    catch(DbException e)
    {
        // do whatever you want with the information from e
    }
    
    Tuesday, April 26, 2011 12:44 PM
  • Hello, firstly, when somebody spend time to try to help you, I think he/she merits a little respect. Here you'll not find "ridiculous solutions", you'll find solutions, advices, proposals, ... wich may or not match with your needs.

    The Louis.fr proposal is right. But I don't think you merit it.

    Bye,

    Miguel.


    Tuesday, April 26, 2011 1:03 PM
  • DbAdapter throws the different exceptions (ArgumentNull, InvalidOperation e.t.c.). Also anyone can implements the DbAdapter class and anyone can throws their exception types in overridden methods. If you want catch all exceptions with using same type; you should use the "System.Exception" type in "catch" statement.
    My suggestion is you should catch all exceptions, and throw again your own exception type, and catch its at one point.
    So, It's not ridiculous.

    Make everything as simple as possible, but not simpler.
    Tuesday, April 26, 2011 2:08 PM
  • Ok, I'll explain:

    DbDataAdapter doesn't throw a DbException from its operations, so I can't catch it the usual way. I'm looking for another way to retrieve it.

    Thanks for any help.

     

    Tuesday, April 26, 2011 4:21 PM
  • If no DbException is thrown, you cannot retrieve it.
    Wednesday, April 27, 2011 7:57 AM
  • Hi OLAPWisdom,

    You can not post duplicated question across some forums. It is a data issue. So it is better for you to post it in Data Platform Development Forum. As there are more experts there. So I will merge them. Thanks for understanding.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Wednesday, April 27, 2011 8:23 AM
  • I thought there might exist another way to retrieve it, maybe a property in which it's stored if the operation failed or something.

    If someone knows, please help me.

     

    Friday, April 29, 2011 9:14 AM
  •  

    Hi OLAPWisdom,

    Welcome!

    @Louis is right, we should catch this exception, Errors occurring at the data source are handled by the DbException code block, and all other exceptions are handled in the Exception block. DbDataAdapter use commands to communicate with database, we just can use catch method to retrieve the Excption.

    Inheritance Hierarchy

    --------------------------------------------------------------------------------

    System.Object

      System.Exception

        System.SystemException

          System.Runtime.InteropServices.ExternalException

            System.Data.Common.DbException

              System.Data.Odbc.OdbcException

              System.Data.OleDb.OleDbException

              System.Data.OracleClient.OracleException

              System.Data.SqlClient.SqlException

    You can refer here about how to handle data errors with DbException: http://msdn.microsoft.com/en-us/library/9hy8csk1.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, April 30, 2011 1:50 AM
    Moderator