Try ... Catch DuplicateKeyException
- 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
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.
- Marked As Answer byYichun_FengMSFT, ModeratorSunday, November 08, 2009 2:00 AM
All Replies
- 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.
- 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 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.
- Marked As Answer byYichun_FengMSFT, ModeratorSunday, November 08, 2009 2:00 AM


