how to identify the OleDb exception without relying on its message

  • Thread starter Thread starter Igor G
  • Start date Start date
I

Igor G

I have written the code that allows to work with a password protected
Microsoft Access database as well as with regular (not password protected)
Access DB in VB. Net. I use OleDb objects. I need to identify whether a
current database is password protected, or not. For this purpose, I create a
regular connection string with no password set, and try to open the
connection. This piece of code is embedded into Try..Catch.. structure. If a
database is password protected, an OleDb exception is raised, with the
message "Not a valid password". To determine what is the reason of failure to
open a connection, I test the message property of the exception. If it
contains the string "Not a valid password", then I can conclude that the
database is password protected.

This code worked fine until we shipped our software to Netherland. I have
found that the message is now "Geen geldig watchwoord", and my code stopped
working.

The question 1: is there a way to identify the particular exception by an
universal attribute (like Error number) without relying onto its message
(which looks different in localized versions of OS)?

The question 2: is there an alternative way to find whether a Microsoft
Access database is password protected?

Thanks

Igor
 
hi!
The System.Data.OleDb.OleDbException exception has a collection of error
objects System.Data.OleDb.OleDbException.Errors which you can use to get, for
instance, the SQLState value of the error.
According to the documentation there is always at least one error object in
this collection.
So catch the exception and verify the error SQLState.

Regarding question #2 : I don't know, sorry.
Jorge C.


Example in C#
catch(System.Data.OleDb.OleDbException oEx)
{
OleDbError error = vException.Errors[0];
switch(error.SQLState) //VB-> select case error.SQLState
{ ....
}
}
 
Back
Top