Smarton
Smarton

Reputation: 119

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

Upvotes: 0

Views: 497

Answers (2)

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3745

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

Upvotes: 2

Benjamin Racette
Benjamin Racette

Reputation: 172

Can you defer the initialization of your dbSource variable to a later time? If so, then put the offending code (assigning a value to dbSource) in a try catch block, catch the exception and display the proper message accordingly.

Upvotes: 1

Related Questions