How do I catch this exception?

blacksaibot

Member
Joined
Mar 22, 2012
Messages
8
Programming Experience
Beginner
System.Data.OleDbException (0x80004005): The changes you requested to the table were not successful because they would create duplicate values in the index, primary key..."


I know how to catch all exceptions. But I want to catch THIS SPECIFIC ONE so I can call one of my subroutines to generate a different ID that will not violate the primary key.


One TEMPORARY solution I've already applied was to query the database to find all existing distinct IDs, and them to a list, and continuously generate an ID that hasn't been used yet by checking if the list .contains it already (via a do while loop). However, I don't want to create a data type that could possibly house thousands of IDs.


Thanks in advance!
 
Try
     ...
Catch ex As System.Data.OleDbException
         Select Case ex.Number
         Case ...
                 ...
         Case Else : Throw
         End Select
End Try
 
Back
Top