none
ErrorCode from OleDbException RRS feed

  • Question

  • Hi, I am new using the System.Data.OleDb .NET Framework Data Provider for OLE DB (I am not quite sure if this is the correct forum. if it is not could someone please point me to the right forum?).

    I am developing an application in C# that is inserting information to a data base. however, sometimes the user wants to insert some values that are already inserted in the database and correspond to the primary key, therefore I get an error:

    System.Data.OleDb.OleDbException was caught
      Message="The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."
      Source="Microsoft JET Database Engine"
      ErrorCode=-2147467259 

    What I would like to do is just to ignore this case (only this case with duplicated values in the primary key) and continue with the program execution assuming that if the value was already there there is no point to bother the user and just continue working. 

    My question is if I can entrust with the ErrorCode=-2147467259 would correspond ONLY to this error?. It is to say, I am making a comparison inside catch about this ErrorCode:

    (The problem is that I was looking for this code in the internet and I found that it also means other kind of database errors).
     
                            SQLCommand.CommandText = "INSERT INTO nproyecto (" + XXX + ") VALUES (" + YYY+ ")";

                            try
                            {
                                int response = SQLCommand.ExecuteNonQuery();
                                if (response < 1)  //JUST IN CASE
                                {
                                    throw MyCustomException("My custom message to be displayed in RunWorkerCompleted");
                                }
                            }
                            catch (OleDbException Ex)
                            { 
                                if (Ex.ErrorCode != -2147467259)     //This code happens ONLY when trying to add duplicated values
                                    throw Ex;                                      //to the primary key in the database, in this case just do nothing
                            }                                                          //and continue loading the other no duplicated values


    I was wondering also if is there a way to disable this error maybe from other part of the program such as the same SQL command?
    Or should I really need to deal with try - catch blocks?

    Thank you very much.
    • Moved by Yichun_Feng Monday, September 28, 2009 2:52 AM Data Provider issue (From:ADO.NET DataSet)
    Wednesday, September 23, 2009 3:02 PM

Answers

  • Hi Kikeman,

     

    For exception handling, the most common way is use try and catch.

    The scope for exception handlers starts with a marker clause (try) and ends in the start of the first handler clause (catch). Several handler clauses can follow, and each can specify which exception types it handles and what name it uses for the exception object.

     

    You can get more information about exception handling from the following URLs:

    http://msdn.microsoft.com/en-us/library/ms229005(VS.80).aspx

    http://en.wikipedia.org/wiki/Exception_handling

     

    There is a thread about ignoring exception. The MVP also suggested use try and catch:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e2e0bea3-0d45-4c25-92dd-fd1f3c348ac6

     

     

    If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kikeman Monday, September 28, 2009 7:11 PM
    Monday, September 28, 2009 2:51 AM
  • Hi Kikeman,

    If you want to restrict some db related exceptions, you have to deal with the Try-Catch block.
    Pls find below your modified sample code.

    try
    {
           int response = SQLCommand.ExecuteNonQuery();
    }                                                    
    catch (OleDbException Ex)
    {                     
         if (Ex.ErrorCode < 1)
               throw MyCustomException("My custom message to be displayed in RunWorkerCompleted");
         
         else if (Ex.ErrorCode != -2147467259)     
         	throw Ex;                                                     
    }
    

    baskaran r
    • Marked as answer by Kikeman Monday, September 28, 2009 7:11 PM
    Monday, September 28, 2009 9:55 AM

All replies

  • Hi Kikeman,

     

    For exception handling, the most common way is use try and catch.

    The scope for exception handlers starts with a marker clause (try) and ends in the start of the first handler clause (catch). Several handler clauses can follow, and each can specify which exception types it handles and what name it uses for the exception object.

     

    You can get more information about exception handling from the following URLs:

    http://msdn.microsoft.com/en-us/library/ms229005(VS.80).aspx

    http://en.wikipedia.org/wiki/Exception_handling

     

    There is a thread about ignoring exception. The MVP also suggested use try and catch:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/e2e0bea3-0d45-4c25-92dd-fd1f3c348ac6

     

     

    If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Kikeman Monday, September 28, 2009 7:11 PM
    Monday, September 28, 2009 2:51 AM
  • Hi Kikeman,

    If you want to restrict some db related exceptions, you have to deal with the Try-Catch block.
    Pls find below your modified sample code.

    try
    {
           int response = SQLCommand.ExecuteNonQuery();
    }                                                    
    catch (OleDbException Ex)
    {                     
         if (Ex.ErrorCode < 1)
               throw MyCustomException("My custom message to be displayed in RunWorkerCompleted");
         
         else if (Ex.ErrorCode != -2147467259)     
         	throw Ex;                                                     
    }
    

    baskaran r
    • Marked as answer by Kikeman Monday, September 28, 2009 7:11 PM
    Monday, September 28, 2009 9:55 AM