Error Handling in exception
-
Can anyone please help me on the error handling? This code below is belongs to SQL. I'm doing in Access. Catch ex As Exception if ex.Number = 2627 <---- ex.Number is not support in system.data.OleDb. Any solution? Please help. Try myCommand.ExecuteNonQuery() Message.Style("color") = "DarkBlue" Message.InnerHtml = "Record is created successfully." Catch ex As SqlException If ex.Number = 2627 Then Message.InnerHtml = "ERROR: A record already exists with " _ & "the same primary key" Else Message.InnerHtml = "ERROR: Could not add record, please " _ & "ensure the fields are correctly filled out" Message.Style("color") = "red" End If End Try
-
Can anyone please help me on the error handling? This code below is belongs to SQL. I'm doing in Access. Catch ex As Exception if ex.Number = 2627 <---- ex.Number is not support in system.data.OleDb. Any solution? Please help. Try myCommand.ExecuteNonQuery() Message.Style("color") = "DarkBlue" Message.InnerHtml = "Record is created successfully." Catch ex As SqlException If ex.Number = 2627 Then Message.InnerHtml = "ERROR: A record already exists with " _ & "the same primary key" Else Message.InnerHtml = "ERROR: Could not add record, please " _ & "ensure the fields are correctly filled out" Message.Style("color") = "red" End If End Try
The exception class
SqlException
exposes a property calledNumber
. TheOleDbException
class does not. If you're using OleDb against an Access database, you'd want to determine how the "primary key" error is reflected inOleDbException
- take a look atOleDbException
in the SDK documentation.OleDbException
does provide anErrorCode
property and anErrors
collection - both may be useful for this purpose. I don't know whatErrorCode
is returned on a "primary key already exists" error, but you could find out easily enough catching theOleDbException
, setting yourMessage.InnerHtml
value to theErrorCode
, and running a test where you try to insert a duplicate record.