Your connection string is invalid. You're missing a
between the provider and the database path.
Second problem: Your database path will only work if your application is at the root of a site. Use an app-relative path instead:
dbSource = HttpContext.Current.Server.MapPath("~/App_Data/CM.mdb")
Third problem: Calling
MsgBox in an ASP.NET application will not work. If you're lucky, you'll get an exception indicating that the current process isn't interactive. If not, you'll pop up a message box on the server, where nobody will ever see it, and your code will hang waiting for someone to hit "OK".
Fourth problem: You haven't actually shown or described how your code is called, or what line the exception is thrown from, but I'm guessing that you're trying to call the code at a point when
HttpContext.Current is not available. Try changing the
MapPath line to:
dbSource = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/CM.mdb")
As per the comment, this code is for a Windows
application, not an ASP.NET application.
Windows applications do not have a current
will always return
. When you try to access
, it will always throw a
Furthermore, Windows applications don't have an
You will need to specify the correct file-system path to your access database. Assuming the database file is in the same folder as your application, you can use
as a shortcut:
Dim con As New OleDb.OleDbConnection
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\CM.mdb;Persist Security Info=False;"
con.ConnectionString = connectionString
MsgBox("Database is now open")
MsgBox("Database is now Closed")
The user running your application will require write access to the directory containing the database file.