OleDbException

  • Thread starter Thread starter Cheung
  • Start date Start date
C

Cheung

Guest
my vb.net application inserts a record in the MS Access db. When I insert a record with duplicate primary key, an oledbexception is thrown. How can I know the reason of the exception? oledbexception.ErrorCode, OleDbError.SQLstate or which property I should refer to? and what is the value of the property? Thanks
 
The whole point of a primary key is to be unique. You cant and shouldnt have two or more with the same value.

Good Luck
-CL
 
Thank for your prompt reply. Perhaps my question was unclear.
Let me re-write my question as follow,

When I insert a record into a table of MS Access , an OleDbException is thrown. How can I know the reason of the exception. I want to customize the warning messages instead of just print out the description message from the exception.
 
Straight from the .NET SDK:

Code:
    Try
        myCommand.Connection.Open()
    Catch myException As OleDbException
        Dim i As Integer
        For i = 0 To myException.Errors.Count - 1
            MessageBox.Show("Index #" + i.ToString() + ControlChars.Cr _
               + "Message: " + myException.Errors(i).Message + ControlChars.Cr _
               + "Native: " + myException.Errors(i).NativeError.ToString() + ControlChars.Cr _
               + "Source: " + myException.Errors(i).Source + ControlChars.Cr _
               + "SQL: " + myException.Errors(i).SQLState + ControlChars.Cr)
        Next i
    End Try

Good Luck
-CL
 
you are really nice.

I was asking how I can know the meaning of the value of the properties of the Error object. For example, when I inserted a record with duplicate PK, the value of the Error.SQLstate is 3022. Is 3022 means "pk already exists in the database"? If yes, I can give out my own warning message instead of printing out Error.Message which may be too complicated for my users. : )
 
Back
Top