Catch "Cannot insert duplicate key row" exception

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    #1

    Catch "Cannot insert duplicate key row" exception

    I really don't like the users getting an unhandled expception page,
    and I'm still to new with ASP.Net and C#. So please accept my
    appology for the 2 part question.

    SqlException (0x80131904)

    1.) Is an "Cannot insert duplicate key row" exception from a FormView
    returned as part of the ItemInserting or ItemInserted event?

    2.) What is the recommended way to catch and deal with an "Cannot
    insert duplicate key row " exception?

    I look at dealing with Exceptions as an Art, and I have a lot of
    respect for those that can do it with very little effort. I hope that
    one of you will be able to help me with this.

    Thanks

  • Mr. Arnold

    #2
    Re: Catch "Cannot insert duplicate key row" exception


    "Dave" <Dave.Burkett@J acobs.comwrote in message
    news:1194180379 .622626.142620@ i13g2000prf.goo glegroups.com.. .
    >I really don't like the users getting an unhandled expception page,
    and I'm still to new with ASP.Net and C#. So please accept my
    appology for the 2 part question.
    >
    SqlException (0x80131904)
    >
    1.) Is an "Cannot insert duplicate key row" exception from a FormView
    returned as part of the ItemInserting or ItemInserted event?
    >
    2.) What is the recommended way to catch and deal with an "Cannot
    insert duplicate key row " exception?
    >
    I look at dealing with Exceptions as an Art, and I have a lot of
    respect for those that can do it with very little effort. I hope that
    one of you will be able to help me with this.
    >
    You page redirect or ServerTransfer to an error page and display the
    message, and you come out of the application gracefully or redirect to a
    Logon page as an example.

    If you have some kind of problem with inserting records into a SQL Server
    table based on a table's primary record-key, you got other problems out the
    gate that you should be dealing with, because this kind of error condition
    should net be happening if the code is solid.


    Comment

    • Dave

      #3
      Re: Catch &quot;Cannot insert duplicate key row&quot; exception

      Mr. Arnold,
      Thanks for the quick reply. I understand what you're saying, and as
      far as my code being solid, that's why I'm trying to capturing the
      duplicate entry error. Redirecting the page once the specific error
      above is caught isn't the issue. The issue is how do I capture the
      specific SqlException (0x80131904) error?

      Also...

      1.) Is an "Cannot insert duplicate key row" exception from a
      FormView
      returned as part of the ItemInserting or ItemInserted event?

      Thanks, and I hope to hear from you soon.





      On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
      "Dave" <Dave.Burk...@J acobs.comwrote in message
      >
      news:1194180379 .622626.142620@ i13g2000prf.goo glegroups.com.. .
      >
      >
      >
      >
      >
      I really don't like the users getting an unhandled expception page,
      and I'm still to new with ASP.Net and C#. So please accept my
      appology for the 2 part question.
      >
      SqlException (0x80131904)
      >
      1.) Is an "Cannot insert duplicate key row" exception from a FormView
      returned as part of the ItemInserting or ItemInserted event?
      >
      2.) What is the recommended way to catch and deal with an "Cannot
      insert duplicate key row " exception?
      >
      I look at dealing with Exceptions as an Art, and I have a lot of
      respect for those that can do it with very little effort. I hope that
      one of you will be able to help me with this.
      >
      You page redirect or ServerTransfer to an error page and display the
      message, and you come out of the application gracefully or redirect to a
      Logon page as an example.
      >
      If you have some kind of problem with inserting records into a SQL Server
      table based on a table's primary record-key, you got other problems out the
      gate that you should be dealing with, because this kind of error condition
      should net be happening if the code is solid.- Hide quoted text -
      >
      - Show quoted text -

      Comment

      • Morten Wennevik [C# MVP]

        #4
        Re: Catch &quot;Cannot insert duplicate key row&quot; exception

        Hi Dave,

        The exception occurs between these events, when the data is attempted inserted in the datagrid's datasource, but if the exception occures, you should be able to see it in the Exception property of the DetailsViewInse rtedEventArgs object. This object also has a ExceptionHandle d property enabling you to consume the exception.


        On Sun, 04 Nov 2007 14:26:07 +0100, Dave <Dave.Burkett@J acobs.comwrote:
        Mr. Arnold,
        Thanks for the quick reply. I understand what you're saying, and as
        far as my code being solid, that's why I'm trying to capturing the
        duplicate entry error. Redirecting the page once the specific error
        above is caught isn't the issue. The issue is how do I capture the
        specific SqlException (0x80131904) error?
        >
        Also...
        >
        1.) Is an "Cannot insert duplicate key row" exception from a
        FormView
        returned as part of the ItemInserting or ItemInserted event?
        >
        Thanks, and I hope to hear from you soon.
        >
        >
        >
        >
        >
        On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
        >"Dave" <Dave.Burk...@J acobs.comwrote in message
        >>
        >news:119418037 9.622626.142620 @i13g2000prf.go oglegroups.com. ..
        >>
        >>
        >>
        >>
        >>
        >I really don't like the users getting an unhandled expception page,
        and I'm still to new with ASP.Net and C#. So please accept my
        appology for the 2 part question.
        >>
        SqlException (0x80131904)
        >>
        1.) Is an "Cannot insert duplicate key row" exception from a FormView
        returned as part of the ItemInserting or ItemInserted event?
        >>
        2.) What is the recommended way to catch and deal with an "Cannot
        insert duplicate key row " exception?
        >>
        I look at dealing with Exceptions as an Art, and I have a lot of
        respect for those that can do it with very little effort. I hope that
        one of you will be able to help me with this.
        >>
        >You page redirect or ServerTransfer to an error page and display the
        >message, and you come out of the application gracefully or redirect to a
        >Logon page as an example.
        >>
        >If you have some kind of problem with inserting records into a SQL Server
        >table based on a table's primary record-key, you got other problems out the
        >gate that you should be dealing with, because this kind of error condition
        >should net be happening if the code is solid.- Hide quoted text -
        >>
        >- Show quoted text -
        >
        >
        >


        --
        Happy coding!
        Morten Wennevik [C# MVP]

        Comment

        • Mr. Arnold

          #5
          Re: Catch &quot;Cannot insert duplicate key row&quot; exception


          "Dave" <Dave.Burkett@J acobs.comwrote in message
          news:1194182767 .214129.213090@ v29g2000prd.goo glegroups.com.. .
          Mr. Arnold,
          Thanks for the quick reply. I understand what you're saying, and as
          far as my code being solid, that's why I'm trying to capturing the
          duplicate entry error. Redirecting the page once the specific error
          above is caught isn't the issue. The issue is how do I capture the
          specific SqlException (0x80131904) error?
          >
          An Exception I know has Innertext messages you might be able to parce. You
          should look at the properties of the Exception.

          catch (SqlException sqlex)

          sqlex.Innertext or something like that -- look at the Exception properties
          to see what you can use to trap on for a specific error.
          >
          1.) Is an "Cannot insert duplicate key row" exception from a
          FormView
          returned as part of the ItemInserting or ItemInserted event?
          I cannot tell you about that one, because I never allow the UI/ASP page to
          make direct contact with a database.


          Comment

          • Chris Mullins [MVP - C#]

            #6
            Re: Catch &quot;Cannot insert duplicate key row&quot; exception

            The code to catch this is really pretty easy. The key is to just re-throw it
            if it's not the error you're looking for.

            try
            {
            bla bla
            }
            catch(SqlExcept ion ex)
            {
            if (!ex.ErrorCode == 0x80131904)
            throw;

            // Put relevant error handling logic here
            }

            --
            Chris Mullins

            "Dave" <Dave.Burkett@J acobs.comwrote in message
            news:1194182767 .214129.213090@ v29g2000prd.goo glegroups.com.. .
            Mr. Arnold,
            Thanks for the quick reply. I understand what you're saying, and as
            far as my code being solid, that's why I'm trying to capturing the
            duplicate entry error. Redirecting the page once the specific error
            above is caught isn't the issue. The issue is how do I capture the
            specific SqlException (0x80131904) error?
            >
            Also...
            >
            1.) Is an "Cannot insert duplicate key row" exception from a
            FormView
            returned as part of the ItemInserting or ItemInserted event?
            >
            Thanks, and I hope to hear from you soon.
            >
            >
            >
            >
            >
            On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
            >"Dave" <Dave.Burk...@J acobs.comwrote in message
            >>
            >news:119418037 9.622626.142620 @i13g2000prf.go oglegroups.com. ..
            >>
            >>
            >>
            >>
            >>
            >I really don't like the users getting an unhandled expception page,
            and I'm still to new with ASP.Net and C#. So please accept my
            appology for the 2 part question.
            >>
            SqlException (0x80131904)
            >>
            1.) Is an "Cannot insert duplicate key row" exception from a FormView
            returned as part of the ItemInserting or ItemInserted event?
            >>
            2.) What is the recommended way to catch and deal with an "Cannot
            insert duplicate key row " exception?
            >>
            I look at dealing with Exceptions as an Art, and I have a lot of
            respect for those that can do it with very little effort. I hope that
            one of you will be able to help me with this.
            >>
            >You page redirect or ServerTransfer to an error page and display the
            >message, and you come out of the application gracefully or redirect to a
            >Logon page as an example.
            >>
            >If you have some kind of problem with inserting records into a SQL Server
            >table based on a table's primary record-key, you got other problems out
            >the
            >gate that you should be dealing with, because this kind of error
            >condition
            >should net be happening if the code is solid.- Hide quoted text -
            >>
            >- Show quoted text -
            >
            >

            Comment

            • Dave

              #7
              Re: Catch &quot;Cannot insert duplicate key row&quot; exception

              Chirs, Would that be under the Inserted Exception?



              On Nov 5, 3:25 pm, "Chris Mullins [MVP - C#]" <cmull...@yahoo .com>
              wrote:
              The code to catch this is really pretty easy. The key is to just re-throw it
              if it's not the error you're looking for.
              >
              try
              {
              bla bla}
              >
              catch(SqlExcept ion ex)
              {
              if (!ex.ErrorCode == 0x80131904)
              throw;
              >
              // Put relevant error handling logic here
              >
              }
              >
              --
              Chris Mullins
              >
              "Dave" <Dave.Burk...@J acobs.comwrote in message
              >
              news:1194182767 .214129.213090@ v29g2000prd.goo glegroups.com.. .
              >
              >
              >
              Mr. Arnold,
              Thanks for the quick reply. I understand what you're saying, and as
              far as my code being solid, that's why I'm trying to capturing the
              duplicate entry error. Redirecting the page once the specific error
              above is caught isn't the issue. The issue is how do I capture the
              specific SqlException (0x80131904) error?
              >
              Also...
              >
              1.) Is an "Cannot insert duplicate key row" exception from a
              FormView
              returned as part of the ItemInserting or ItemInserted event?
              >
              Thanks, and I hope to hear from you soon.
              >
              On Nov 4, 7:13 am, "Mr. Arnold" <MR. Arn...@Arnold.c omwrote:
              "Dave" <Dave.Burk...@J acobs.comwrote in message
              >
              >news:119418037 9.622626.142620 @i13g2000prf.go oglegroups.com. ..
              >
              I really don't like the users getting an unhandled expception page,
              and I'm still to new with ASP.Net and C#. So please accept my
              appology for the 2 part question.
              >
              SqlException (0x80131904)
              >
              1.) Is an "Cannot insert duplicate key row" exception from a FormView
              returned as part of the ItemInserting or ItemInserted event?
              >
              2.) What is the recommended way to catch and deal with an "Cannot
              insert duplicate key row " exception?
              >
              I look at dealing with Exceptions as an Art, and I have a lot of
              respect for those that can do it with very little effort. I hope that
              one of you will be able to help me with this.
              >
              You page redirect or ServerTransfer to an error page and display the
              message, and you come out of the application gracefully or redirect to a
              Logon page as an example.
              >
              If you have some kind of problem with inserting records into a SQL Server
              table based on a table's primary record-key, you got other problems out
              the
              gate that you should be dealing with, because this kind of error
              condition
              should net be happening if the code is solid.- Hide quoted text -
              >
              - Show quoted text -- Hide quoted text -
              >
              - Show quoted text -

              Comment

              Working...