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. General Programming
  3. C#
  4. Catch Specific exception in SqlClient

Catch Specific exception in SqlClient

Scheduled Pinned Locked Moved C#
databasehelpquestioncsharpalgorithms
7 Posts 4 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.
  • S Offline
    S Offline
    Skanless
    wrote on last edited by
    #1

    I am writing an HR application to add new employees. Currently, I am having an issue where I want to send a specific message base on the exception that is received. In my database SNN is a unique key hence if an SSN is being added that already exist in the DB a Unique key violation exception is thrown. If the UserID already exist a Primary Key violation exception is thrown. I am certain this is possible but just not sure what is best practice. I can write a method which search the DB first and if the record is found thrown an error message based on whether it was the Unique key violation or the PK violation. I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message. Any advice will be greatly appreciated.

    Skan If you knew it would not compile why didn't you tell me?!?!?!

    C E P 3 Replies Last reply
    0
    • S Skanless

      I am writing an HR application to add new employees. Currently, I am having an issue where I want to send a specific message base on the exception that is received. In my database SNN is a unique key hence if an SSN is being added that already exist in the DB a Unique key violation exception is thrown. If the UserID already exist a Primary Key violation exception is thrown. I am certain this is possible but just not sure what is best practice. I can write a method which search the DB first and if the record is found thrown an error message based on whether it was the Unique key violation or the PK violation. I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message. Any advice will be greatly appreciated.

      Skan If you knew it would not compile why didn't you tell me?!?!?!

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Skanless wrote:

      I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message

      All the Ids can be found in SQL Server itself. SELECT * FROM sys.messages or SELECT * FROM sysmessages (if you are on SQL Server 2000)


      Upcoming FREE developer events: * Developer Day Scotland My website

      S 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Skanless wrote:

        I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message

        All the Ids can be found in SQL Server itself. SELECT * FROM sys.messages or SELECT * FROM sysmessages (if you are on SQL Server 2000)


        Upcoming FREE developer events: * Developer Day Scotland My website

        S Offline
        S Offline
        Skanless
        wrote on last edited by
        #3

        Thanks dude, I found them. I would still like to know what best practice for a situation like this.

        Skan If you knew it would not compile why didn't you tell me?!?!?!

        1 Reply Last reply
        0
        • S Skanless

          I am writing an HR application to add new employees. Currently, I am having an issue where I want to send a specific message base on the exception that is received. In my database SNN is a unique key hence if an SSN is being added that already exist in the DB a Unique key violation exception is thrown. If the UserID already exist a Primary Key violation exception is thrown. I am certain this is possible but just not sure what is best practice. I can write a method which search the DB first and if the record is found thrown an error message based on whether it was the Unique key violation or the PK violation. I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message. Any advice will be greatly appreciated.

          Skan If you knew it would not compile why didn't you tell me?!?!?!

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Use IF Exists in a trigger and then throw custom exceptions from the sp. However, SQL server will natively through the message number so there is no reason for custom handling to catch it.

          Need a C# Consultant? I'm available.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

          S 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            Use IF Exists in a trigger and then throw custom exceptions from the sp. However, SQL server will natively through the message number so there is no reason for custom handling to catch it.

            Need a C# Consultant? I'm available.
            Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway

            S Offline
            S Offline
            Skanless
            wrote on last edited by
            #5

            This worked perfectly!!! IF EXISTS (SELECT Emp_Login_ID FROM Employees where Emp_Login_ID = @Emp_Login_ID ) RETURN -1 ELSE IF EXISTS (SELECT Emp_SSN FROM Employees where Emp_SSN = @Emp_SSN ) RETURN -2 ELSE INSERT INTO........ I am no longer receiving the Exception. However, I am not sure how to throw the Error message in C#. How would C# know to throw and Exception if the insert does nto execute? Thanks for you help. Note: All this is done in a single stored procedure. Which is then called in the application in a try, catch statement.

            Skan If you knew it would not compile why didn't you tell me?!?!?!

            modified on Thursday, December 06, 2007 12:44:52 AM

            S 1 Reply Last reply
            0
            • S Skanless

              This worked perfectly!!! IF EXISTS (SELECT Emp_Login_ID FROM Employees where Emp_Login_ID = @Emp_Login_ID ) RETURN -1 ELSE IF EXISTS (SELECT Emp_SSN FROM Employees where Emp_SSN = @Emp_SSN ) RETURN -2 ELSE INSERT INTO........ I am no longer receiving the Exception. However, I am not sure how to throw the Error message in C#. How would C# know to throw and Exception if the insert does nto execute? Thanks for you help. Note: All this is done in a single stored procedure. Which is then called in the application in a try, catch statement.

              Skan If you knew it would not compile why didn't you tell me?!?!?!

              modified on Thursday, December 06, 2007 12:44:52 AM

              S Offline
              S Offline
              Skanless
              wrote on last edited by
              #6

              Thanks for the help guys. I got it working with the following lines of code. I will simplify my code later but just need to get it working.

              SqlParameter paramReturnValue = new SqlParameter();
              paramReturnValue.ParameterName = "@return_value";
              paramReturnValue.SqlDbType = SqlDbType.Int;
              paramReturnValue.Direction = ParameterDirection.ReturnValue;

              cmd.Parameters.Add(paramReturnValue);

              Skan If you knew it would not compile why didn't you tell me?!?!?!

              1 Reply Last reply
              0
              • S Skanless

                I am writing an HR application to add new employees. Currently, I am having an issue where I want to send a specific message base on the exception that is received. In my database SNN is a unique key hence if an SSN is being added that already exist in the DB a Unique key violation exception is thrown. If the UserID already exist a Primary Key violation exception is thrown. I am certain this is possible but just not sure what is best practice. I can write a method which search the DB first and if the record is found thrown an error message based on whether it was the Unique key violation or the PK violation. I have also been searching MSDN to see if I can identify the C# SqlClient error ID for either scenarios and based on the exception ID, alert the user with the right error message. Any advice will be greatly appreciated.

                Skan If you knew it would not compile why didn't you tell me?!?!?!

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                A System.Data.SqlClient.SqlException has an Errors array which contains the specific error(s). I search it for 2601 (unique index violation) or 2627 (primary key violation), and if present I wrap it in a custom exception. You can take a look at my DatabaseAccessor article which contains an enum of the code I support.

                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