First problem: Your connection string is invalid. You're missing a
Data Source=
between the provider and the database path.
http://www.connectionstrings.com/access/[
^]
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")
EDIT: As per the comment, this code is for a
Windows application, not an ASP.NET application.
Windows applications do not have a current
HttpContext
.
HttpContext.Current
will always return
Nothing
. When you try to access
HttpContext.Current.Server
, it will always throw a
NullReferenceException
.
Furthermore, Windows applications don't have an
App_Data
folder.
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
|DataDirectory|
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
con.Open()
MsgBox("Database is now open")
con.Close()
MsgBox("Database is now Closed")
NB: The user running your application will require write access to the directory containing the database file.