Catch duplicate primary key exeption

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

    Catch duplicate primary key exeption

    The code below can only catch common excption.
    I want to catch duplicate primary key exeption and do something more.

    How can I catch duplicate primary key exeption

    try
    {
    Insert a record to table
    }
    catch (????)
    {
    Do some thing more
    }
    catch (Exception err)
    {
    Label1.Text=err .Message;
    }


  • Marina

    #2
    Re: Catch duplicate primary key exeption

    You catch a SqlException. Although that can be thrown for any number of
    reasons. You would have to examine the collection of errors inside it, and
    see the number to see exactly why the exception was thrown.

    "ad" <ad@wfes.tcc.ed u.tw> wrote in message
    news:%23tPNSzpa FHA.2128@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    > The code below can only catch common excption.
    > I want to catch duplicate primary key exeption and do something more.
    >
    > How can I catch duplicate primary key exeption
    >
    > try
    > {
    > Insert a record to table
    > }
    > catch (????)
    > {
    > Do some thing more
    > }
    > catch (Exception err)
    > {
    > Label1.Text=err .Message;
    > }
    >
    >[/color]


    Comment

    • hashimisayed@gmail.com

      #3
      Re: Catch duplicate primary key exeption

      How is the primary key created? Is it auto generated? If so, you
      wouldn't have this problem, so I am guessing that you are generating it
      yourself. Can you tell us how the key is generated? You also need to
      tell us how you are doing the insert? Is all of this done in code,
      i.e., do you create a key in code and then try to do an insert? Or do
      you use stored procedures for all of this? Tell us a little bit more...

      sayed

      Comment

      • ad

        #4
        Re: Catch duplicate primary key exeption

        Thanks,
        It is a key like social insurance ID.
        It will be keyin by user.

        <hashimisayed@g mail.com>
        ???????:1118082 382.232840.3249 30@g44g2000cwa. googlegroups.co m...[color=blue]
        > How is the primary key created? Is it auto generated? If so, you
        > wouldn't have this problem, so I am guessing that you are generating it
        > yourself. Can you tell us how the key is generated? You also need to
        > tell us how you are doing the insert? Is all of this done in code,
        > i.e., do you create a key in code and then try to do an insert? Or do
        > you use stored procedures for all of this? Tell us a little bit more...
        >
        > sayed
        >[/color]


        Comment

        • hashimisayed@gmail.com

          #5
          Re: Catch duplicate primary key exeption

          If you are doing the insert via a stored procedure, you can do a
          "count(id) from mytable" and see if that is greater than zero. If so,
          do a "raise error 'social insurance id already exists'". If you are not
          inserting with a stored procedure, you have to do the count statment or
          a select statement to figure out if the social exists and throw a
          similar exception.

          sayed

          Comment

          Working...