Application.StartupPath Problem. [Resolved]
Instead of setting a static path: "C:\Project\...\...\",
I use "Application.StartupPath" to set the default path of the application dynamically during run-time.
It works well all the while, but I encounter some problem in the situation below:
I use an "OleDbConnection" to set a connection to Access database. And the connection string below is created:
Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=D:\Albert\Visual Basic .NET\Address Book\bin\Database\Database.mdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
I try to change the Data Source of this OleDbConnection to refer to "Application.StartupPath & \Database\Database.mdb"
because not every PC have this same path.
This changes is done in the View Code mode under "Windows Form Designer generated code" region.
After changing, the "OleDbConnection" connection string is like below:
Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=" & Application.StartupPath "\Database\Database.mdb;Mode=Share Deny None;Extended Properties="";Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB:Database Password="";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False
When I run the program, the error occurs showing that the database cannot be found.
When I debugged it deeper, I found that the Data Source is not refer to "Application.StartupPath\Database\Database.mdb"
It actually refer to "C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\Database\Database.mdb"
Why is this so? How can I solve this problem?
Please guide. Thank you!
Re: Application.StartupPath Problem.
I didn't read the whole post but is this what you're talking about!
VB Code:
Dim MyPath as string =application.startup & "\filename.xxx"
Dim MyPassword as string =nothing
Private My_Connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath & ";Jet OLEDB:Database Password=" & MyPassword)
Re: Re: Application.StartupPath Problem.
Little change :D
VB Code:
Dim MyPath As String = Application.StartupPath & "\__mydb__.mdb"
Dim MyPassword As String = nothing
don't need app.startup path
If your db resides in the same folder as the exe then you don't need application.startuppath. Just use the name of the db. Also you should be careful with Application.StartupPath as I understand it this is NOT the path to the exe but the Start In path in the shortcut. This defaults to the app.path(vb6) when the path in the shortcut is blank, however, .....
Russ