Ask a questionAsk a question
 

AnswerTry ... Catch DuplicateKeyException

  • Saturday, October 31, 2009 8:50 PMLee_37122 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have seen posts on using 'DuplicateKeyException' for Try ... Catch in C#. However, it does not seem to work for VB. Is it not supported in VB?

Answers

  • Friday, November 06, 2009 7:02 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Lee,

     

    DuplicateKeyException is thrown when an attempt is made to add an object to the identity cache by using a key that is already being used.

     

    That means, it is thrown when you insert two records with the same PK into the context. For example,

     

            Try

                Dim context As New DataClasses1DataContext

                Dim s As New Stu

                s.id = 1

     

                context.Stus.InsertOnSubmit(s)

     

                Dim s1 As New Stu

                s1.id = 1

     

                context.Stus.InsertOnSubmit(s1)

     

                context.SubmitChanges()

            Catch ex As DuplicateKeyException

                Console.WriteLine(ex.ToString)

            End Try

     

    If the record you want to insert has the same PK in DB, the exception is SqlException. You can try it in C#,

     

                try

                {

     

                    DataClasses1DataContext context = new DataClasses1DataContext();

     

                    Stu s = new Stu();

                    s.id = 1;

     

                    context.Stus.InsertOnSubmit(s);

     

     

                    context.SubmitChanges();

                }

                catch (DuplicateKeyException ex)

                {

                    Console.WriteLine(ex.ToString());

                }

                catch (SqlException ex)

                {

                    Console.WriteLine(ex.ToString());

                }

     

    So, the problem is not due to the language differences, but the Exception type.

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Tuesday, November 03, 2009 7:15 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Lee,

    It does support in VB. You can refer to this MSDN document,
    http://msdn.microsoft.com/en-us/library/system.data.linq.duplicatekeyexception.aspx

    If you want to use it, first add reference to System.Data.Linq dll to your project. Then "Imports System.Data.Linq"

    Could you please give the detail code that you are using ?


    Best Regards
    Yichun Feng
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Tuesday, November 03, 2009 9:45 PMLee_37122 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yichen,

    System.Data.Linq was/is listed as a Reference.
    The following are extracts of the Try ...Catch that does not catch Duplicate Key Exceptions

    *****

    Imports

     

    System

    Imports

     

    System.Collections

    Imports

     

    System.Collections.Generic

    Imports

     

    System.Data.Linq

    Imports

     

    System.Linq

    Imports

     

    sqlLineImportNS.linqDataClassesDataContext

    *****

     

    Try

    sqlGPdc.PersonImportAutoIds.InsertOnSubmit(piar)

    sqlGPdc.SubmitChanges(ConflictMode.ContinueOnConflict)

     

    Catch ex As ChangeConflictException

    DisplayConcurrencyCatch(

    "AppendPersonImportAutoId", ex, eDisplayField.ePerson)

     

    Catch ex As DuplicateKeyException

    DisplayDuplicateKeyCatch(

    "AppendPersonImportAutoId", ex, eDisplayField.ePerson)

     

    Catch ex As Exception

    DisplayLog(eDisplay.eCatch,

    "AppendPersonImportAutoId: " & ex.Message)

     

    End Try

    *****

     



    I have narrowed the problem to occur when a 'Drop & Create' and 'Reseed' to 0 is executed by another application.
    The problem still exists when I add the 'Drop & Create' and 'Reseed' code to this application.

    ChangeConflictException did not require any additional definitions and did catch conflicts. DuplicateKeyException selection was available by IntelleSence for selection. Does it require the object class to be added to the project?

    Thanks in advance for your continued support.
    Lee_37122

  • Friday, November 06, 2009 7:02 AMYichun_FengMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Lee,

     

    DuplicateKeyException is thrown when an attempt is made to add an object to the identity cache by using a key that is already being used.

     

    That means, it is thrown when you insert two records with the same PK into the context. For example,

     

            Try

                Dim context As New DataClasses1DataContext

                Dim s As New Stu

                s.id = 1

     

                context.Stus.InsertOnSubmit(s)

     

                Dim s1 As New Stu

                s1.id = 1

     

                context.Stus.InsertOnSubmit(s1)

     

                context.SubmitChanges()

            Catch ex As DuplicateKeyException

                Console.WriteLine(ex.ToString)

            End Try

     

    If the record you want to insert has the same PK in DB, the exception is SqlException. You can try it in C#,

     

                try

                {

     

                    DataClasses1DataContext context = new DataClasses1DataContext();

     

                    Stu s = new Stu();

                    s.id = 1;

     

                    context.Stus.InsertOnSubmit(s);

     

     

                    context.SubmitChanges();

                }

                catch (DuplicateKeyException ex)

                {

                    Console.WriteLine(ex.ToString());

                }

                catch (SqlException ex)

                {

                    Console.WriteLine(ex.ToString());

                }

     

    So, the problem is not due to the language differences, but the Exception type.

     

     

    Does this work for you? If you have any questions or concerns, please update the thread and we will have a further discussion.

     

     

    Best Regards

    Yichun Feng


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.