Smarton Smarton - 6 months ago 23
Vb.net Question

How to give custom error message if file not found

I have a module in vb.net as shown below

Module Module1
Public dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
Public dbSource = "Data Source = C:\PMDatabase\MyDB.mdb"
End Module


it loads database based on the location I set in dbSource , the problem is when file is not Found it give an error message exposing the expected location of database file as shown below

Could not find file 'C:\PMDatabase\MyDB.mdb'. Microsoft.Jet Database Engine

I want the error message to simply show "Database not found"

Thanks

Answer

You can use Exception Handling (Try Catch) like this :

Try

    'your code (Open database).

Catch ex As Exception
    MessageBox.Show("Database not found")
End Try

You can keep the Exception line as well. (You can have as many Catch parts as you want.) This will Catch any other errors that may occur:

Try

    'your code (Open database).

Catch ex As OleDbException
    MessageBox.Show("Database not found")
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
Comments