Click here to Skip to main content
15,891,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to login using server path. But i am not to able to.
VB
Dim con As New OleDb.OleDbConnection
        Dim dbProvider As String
        Dim dbSource As String
        '   Dim info As DirectoryInfo = New DirectoryInfo("App_Data\CM.mdb")

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = HttpContext.Current.Server.MapPath("/App_Data/CM.mdb")

        con.ConnectionString = dbProvider & dbSource
        con.Open()

        MsgBox("Database is now open")

        con.Close()

        MsgBox("Database is now Closed")


i am getting an error saying Null Reference Exception was handled.

Plz help..

Regards,
Megha
Posted
Updated 28-Jul-14 23:16pm
v2
Comments
megha sarath 29-Jul-14 5:39am    
Plz help..

1 solution

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:
VB.NET
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:
VB.NET
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:

VB
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.
 
Share this answer
 
v2
Comments
megha sarath 30-Jul-14 1:29am    
it is giving an error saying HostingEnvironment is not a member of hosting.. Plz Help..
And i want this in vb.net windows application..
Richard Deeming 30-Jul-14 6:54am    
Of course, I should have guessed that a code snipped with no additional information using APIs which only work in ASP.NET was actually from a Windows application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900