Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Server exception codes
Message
 
 
To
All
General information
Forum:
C#
Category:
ADO.NET
Title:
SQL Server exception codes
Miscellaneous
Thread ID:
01653516
Message ID:
01653516
Views:
39
Hi everybody,

In VFP we have the following code for failing tableupdate():
LOCAL laError, lcError
         DECLARE laError[1]               &&what was the error?
         AERROR(laError)

         TABLEREVERT(.T.)               &&get out of trouble...
         FLUSH
         DO CASE
            CASE BETWEEN(laError[1],108,109)   &&file or record lock
               lcError='211'
               .nErrorStatus=0               &&error event will fire here, this supresses it
               .cErrorStatusMsg=''
            CASE laError[1]=1582            &&database rule violation
               lcError='212'
               .nErrorStatus=0
               .cErrorStatusMsg=''            && a good example is violating a database rule like guests.guest_no=0
            CASE laError[1]=1585            && update conflict
               lcError='212'
               .nErrorStatus=0
               .cErrorStatusMsg=''
            CASE laError[1]=1884            && primary key violation
               lcError='213'
               .nErrorStatus=0
               .cErrorStatusMsg=''
            CASE laError[1]=1526 AND;
                  AT('PRIMARY KEY', UPPER(laError[2]))>0 && SQL Server primary key violation
               lcError='213'
               .nErrorStatus=0
               .cErrorStatusMsg=''
            CASE laError[1]=1526            && any other SQL Server error
               lcError='400'
               .nErrorStatus=0
               .cErrorStatusMsg=''
            OTHERWISE                     && all other errors
               lcError='219'
         ENDCASE
However, when I execute C# code like this:
this.lastExecution.RowsReturned = sqlCommand.ExecuteNonQuery();
I get the error number 2627 for the primary key violation error.

Do you know how should I translate the code above into similar code in C#. I wrote:
switch (ex.Number)
            {
                case 1582: // Database rule violation
                    {
                        errorState.ErrorCode = 212;
                        break;
                    }
                case 1585: // update conflict
                    {
                        errorState.ErrorCode = 212;
                        break;
                    }

                case 1884: // primary key violation
                    {
                        errorState.ErrorCode = 213;
                        break;
                    }

                case 1526: 
                    {
                        if (errorState.ErrorMessage.ToUpper().Contains("PRIMARY KEY")) // primary key violation
                        {
                            errorState.ErrorCode = 213;
                        }
                        else
                        {
                            errorState.ErrorCode = 400; // any other error
                        }
                        break;
                    }

                default:
                    {
                        errorState.ErrorCode = 219;
                        break;
                    }
            }
but the error numbers returned by VFP do not seem to match errors returned in C#.

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Reply
Map
View

Click here to load this message in the networking platform