Home All Groups Group Topic Archive Search About

How do I catch duplicate record exception?

Author
19 Aug 2006 7:19 AM
Hexman
Hello All,

How do I catch duplicate record  attempted to be added exception?  As in:

            Catch ex As Exception


Thanks,

Hexman

Author
20 Aug 2006 12:14 PM
scorpion53061
Catch ex As Exception
            If InStr(1, ex.ToString, "duplicate key") > 0 Then

                lblMessage.Text = " User already exists cannot insert
duplicate value."

            Else

                lblMessage.Text = ex.ToString()

            End If

        Finally
            'close the connection here, the Finally part will always
execute
            Conn.Close()
Hexman wrote:
Show quoteHide quote
> Hello All,
>
> How do I catch duplicate record  attempted to be added exception?  As in:
>
>             Catch ex As Exception
>
>
> Thanks,
>
> Hexman
Author
20 Aug 2006 7:25 PM
Hexman
Thanks Scorpion,

I just thought there would be a error number that could be tested for this condition, like H_Result.

Hexman



Show quoteHide quote
On 20 Aug 2006 05:14:35 -0700, "scorpion53061" <ad***@kjmsolutions.com> wrote:

>Catch ex As Exception
>            If InStr(1, ex.ToString, "duplicate key") > 0 Then
>
>                lblMessage.Text = " User already exists cannot insert
>duplicate value."
>
>            Else
>
>                lblMessage.Text = ex.ToString()
>
>            End If
>
>        Finally
>            'close the connection here, the Finally part will always
>execute
>            Conn.Close()
>Hexman wrote:
>> Hello All,
>>
>> How do I catch duplicate record  attempted to be added exception?  As in:
>>
>>             Catch ex As Exception
>>
>>
>> Thanks,
>>
>> Hexman
Author
22 Aug 2006 12:14 PM
Phill W.
Hexman wrote:

> How do I catch duplicate record  attempted to be added exception?  As in:
>
>             Catch ex As Exception

Start with this, generate the Exception and see what you get.

? ex.GetType().ToString()

Assuming this isn't just System.Exception, then change the Catch to
catch the required Type and examine each property of that subclass,
something like

Catch ex As ODBCException

? ex.Errors.Count
? ex.InnerException.ToString()

and so on.  If you're lucky, you'll find something that identifies a
duplicate record exception explicitly.  If not, you'll have to fall back
on looking for some text in the Message.  This will work but isn't
ideal; the message generated could change with, say, an upgrade to the
DBMS software, thereby breaking your program.

HTH,
    Phill  W.