Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. I feel so dirty

I feel so dirty

Scheduled Pinned Locked Moved The Weird and The Wonderful
toolshelpquestion
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Online
    P Online
    PIEBALDconsult
    wrote on last edited by
    #1

    I just stuck this in a small data import utility that I don't particularly care about:

    catch ( System.Data.DataException err )
    {
    if ( !err.ToString().Contains ( "Cannot insert duplicate key" ) )
    {
    throw ;
    }
    }

    If they don't care enough to send me clean data, why should I spend a Friday afternoon writing good code?

    M M X 3 Replies Last reply
    0
    • P PIEBALDconsult

      I just stuck this in a small data import utility that I don't particularly care about:

      catch ( System.Data.DataException err )
      {
      if ( !err.ToString().Contains ( "Cannot insert duplicate key" ) )
      {
      throw ;
      }
      }

      If they don't care enough to send me clean data, why should I spend a Friday afternoon writing good code?

      M Offline
      M Offline
      Mike Prof Chuck
      wrote on last edited by
      #2

      that's the way to do it... why should always we developers have the pressure of being exact and clean :D would've done it too this way... if it was friday afternoon in the mid of summer ;P

      1 Reply Last reply
      0
      • P PIEBALDconsult

        I just stuck this in a small data import utility that I don't particularly care about:

        catch ( System.Data.DataException err )
        {
        if ( !err.ToString().Contains ( "Cannot insert duplicate key" ) )
        {
        throw ;
        }
        }

        If they don't care enough to send me clean data, why should I spend a Friday afternoon writing good code?

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        PIEBALDconsult wrote:

        err.ToString()

        Whatever happened to err.Message ? ;) Marc

        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

        P 1 Reply Last reply
        0
        • M Marc Clifton

          PIEBALDconsult wrote:

          err.ToString()

          Whatever happened to err.Message ? ;) Marc

          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Nah, I tried that first, but the text is in an InnerException. I have code that spelunks the Exceptions looking for the actual Error code from SQL Server, but I didn't have it handy.

          Richard DeemingR 1 Reply Last reply
          0
          • P PIEBALDconsult

            Nah, I tried that first, but the text is in an InnerException. I have code that spelunks the Exceptions looking for the actual Error code from SQL Server, but I didn't have it handy.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            If it's in the innermost exception:

            catch ( System.Data.DataException err )
            {
            if ( !err.GetBaseException().Message.Contains ( "Cannot insert duplicate key" ) )
            {
            throw;
            }
            }

            Exception.GetBaseException Method | MSDN[^] If it can be anywhere in the chain:

            catch ( System.Data.DataException err )
            {
            bool isDuplicateKey = false;
            Exception currentErr = err;

            while (currentErr != null && !isDuplicateKey)
            {
                isDuplicateKey = currentErr.Message.Contains ( "Cannot insert duplicate key" );
                currentErr = currentErr.InnerException;
            }
            
            if ( !isDuplicateKey )
            {
                throw;
            }
            

            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            L P 2 Replies Last reply
            0
            • Richard DeemingR Richard Deeming

              If it's in the innermost exception:

              catch ( System.Data.DataException err )
              {
              if ( !err.GetBaseException().Message.Contains ( "Cannot insert duplicate key" ) )
              {
              throw;
              }
              }

              Exception.GetBaseException Method | MSDN[^] If it can be anywhere in the chain:

              catch ( System.Data.DataException err )
              {
              bool isDuplicateKey = false;
              Exception currentErr = err;

              while (currentErr != null && !isDuplicateKey)
              {
                  isDuplicateKey = currentErr.Message.Contains ( "Cannot insert duplicate key" );
                  currentErr = currentErr.InnerException;
              }
              
              if ( !isDuplicateKey )
              {
                  throw;
              }
              

              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Though I can see the improvement; wouldn't the weak spot here being the check for a localized message? IIRC, then different (localized) versions of .NET will give different results. "Kan geen rij met dubbele sleutel invoegen in object 'dbo.aa673 met unieke index 'IX_673'." Next you'll hear "It worked on Win7, and then I opened it on another machine and it crashed. We didn't change anything and it should still be working. Why does Windows do that?" :laugh:

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              Richard DeemingR 1 Reply Last reply
              0
              • L Lost User

                Though I can see the improvement; wouldn't the weak spot here being the check for a localized message? IIRC, then different (localized) versions of .NET will give different results. "Kan geen rij met dubbele sleutel invoegen in object 'dbo.aa673 met unieke index 'IX_673'." Next you'll hear "It worked on Win7, and then I opened it on another machine and it crashed. We didn't change anything and it should still be working. Why does Windows do that?" :laugh:

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #7

                Indeed. Checking the SQL error codes would be a more robust approach. However, it sounds like this is a program that's only ever run on one computer, so it's unlikely to suddenly start throwing errors in Dutch. :)


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                L 1 Reply Last reply
                0
                • Richard DeemingR Richard Deeming

                  If it's in the innermost exception:

                  catch ( System.Data.DataException err )
                  {
                  if ( !err.GetBaseException().Message.Contains ( "Cannot insert duplicate key" ) )
                  {
                  throw;
                  }
                  }

                  Exception.GetBaseException Method | MSDN[^] If it can be anywhere in the chain:

                  catch ( System.Data.DataException err )
                  {
                  bool isDuplicateKey = false;
                  Exception currentErr = err;

                  while (currentErr != null && !isDuplicateKey)
                  {
                      isDuplicateKey = currentErr.Message.Contains ( "Cannot insert duplicate key" );
                      currentErr = currentErr.InnerException;
                  }
                  
                  if ( !isDuplicateKey )
                  {
                      throw;
                  }
                  

                  }


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  P Online
                  P Online
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  When I do it right, I walk the InnerExceptions to find the first System.Data.SqlClient.SqlException with one of the following error codes:

                          errorcodes = new System.Collections.Generic.Dictionary() ;
                  
                          errorcodes \[ 1205 \] = PIEBALD.Data.ErrorCode.Deadlock             ;
                          errorcodes \[ 2601 \] = PIEBALD.Data.ErrorCode.Duplicate            ;
                          errorcodes \[ 2627 \] = PIEBALD.Data.ErrorCode.Duplicate            ;
                          errorcodes \[  547 \] = PIEBALD.Data.ErrorCode.ReferentialIntegrity ;
                          errorcodes \[   -2 \] = PIEBALD.Data.ErrorCode.Timeout              ;
                  

                  Then wrap it in a custom Exception:

                  public partial class DuplicateException : PIEBALD.Data.OperationFailedException ...
                  
                  public partial class ReferentialIntegrityException : PIEBALD.Data.OperationFailedException ...
                  
                  public partial class TimeoutException : PIEBALD.Data.OperationFailedException ...
                  

                  Then my code can catch and act on specific error codes.

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Indeed. Checking the SQL error codes would be a more robust approach. However, it sounds like this is a program that's only ever run on one computer, so it's unlikely to suddenly start throwing errors in Dutch. :)


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Richard Deeming wrote:

                    However, it sounds like this is a program that's only ever run on one computer

                    I wish I had gotten a penny everytime that assumption fell apart :)

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      I just stuck this in a small data import utility that I don't particularly care about:

                      catch ( System.Data.DataException err )
                      {
                      if ( !err.ToString().Contains ( "Cannot insert duplicate key" ) )
                      {
                      throw ;
                      }
                      }

                      If they don't care enough to send me clean data, why should I spend a Friday afternoon writing good code?

                      X Offline
                      X Offline
                      Xmen Real
                      wrote on last edited by
                      #10

                      What if locale is not english

                      TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L %^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2 W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN% R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-i’TV.C\y<pŠjxsg-b$f4ia>

                      ----------------------------------------------- 128 bit encrypted signature, crack if you can

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups