Results 1 to 10 of 10

Thread: Application.StartupPath Problem. [Resolved]

  1. #1

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question 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 OLEDBatabase Password="";Jet OLEDB:Engine Type=5;Jet OLEDBatabase 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 OLEDBon'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 OLEDBatabase Password="";Jet OLEDB:Engine Type=5;Jet OLEDBatabase 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 OLEDBon'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!
    Last edited by albertlse; Aug 21st, 2003 at 08:34 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Application.StartupPath Problem.

    I didn't read the whole post but is this what you're talking about!
    VB Code:
    1. Dim MyPath as string =application.startup & "\filename.xxx"
    2. Dim MyPassword as string =nothing
    3.  
    4. Private My_Connection As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyPath & ";Jet OLEDB:Database Password=" & MyPassword)

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Re: Application.StartupPath Problem.

    Little change

    VB Code:
    1. Dim MyPath As String = Application.StartupPath & "\__mydb__.mdb"
    2. Dim MyPassword As String = nothing

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin;Data Source=" & Application.StartupPath "\Database\Database.mdb;Mode=Share Deny
    You hamve missed & bettween Application.StartupPath and "\Database\...
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Smile

    It is working fine now! Thank you.

  6. #6

    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

  7. #7
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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.
    Noooooooooooo!
    That works untill you have not changed the parent directory. It may happen that you change it in your application without you being noticed and so it will fail. I have faced the situation and dont recommend this at all. And here was the situation.
    1. Loaded the database and filled some dataset
    2. Showed the report using that dataset and Crystal Reports
    3. Exported the report, for example to C:\
    4. Closed the report and tryed to load the database again. It failed

    Here it looks in C:\ for my database cause now the parent directory has changed without you even thinking that it has changed.

    So always try the full path.

    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, .....
    never faced any problem with it, can you bring a smaple that makes problem?
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  8. #8

    Thread Starter
    Registered User
    Join Date
    Apr 2003
    Location
    Klang, Selangor, Malaysia
    Posts
    163

    Question

    Problem again!!!

    I've change the Connection String of the Database Connection to the following one:

    (changed in "Windows Form Designer generated code" region)

    Me.EntryConnection.ConnectionString = "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:Registr" & _
    "y Path="""";Jet OLEDBatabase Password="""";Jet OLEDB:Engine Type=5;Jet OLEDBatab" & _
    "ase Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Tra" & _
    "nsactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=" & _
    "False;Jet OLEDB:Encrypt Database=False;Jet OLEDBon't Copy Locale on Compact=Fa" & _
    "lse;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"

    It works well after changing just for a while only!!!

    Unfortunately, without my notice, the Connection String change to the following one:

    Me.EntryConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=" & _
    "C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\Database\Database.mdb;Mode=Share" & _
    " Deny None;Extended Properties="""";Jet OLEDB:System database="""";Jet OLEDB:Registr" & _
    "y Path="""";Jet OLEDBatabase Password="""";Jet OLEDB:Engine Type=5;Jet OLEDBatab" & _
    "ase Locking Mode=1;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Tra" & _
    "nsactions=1;Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=" & _
    "False;Jet OLEDB:Encrypt Database=False;Jet OLEDBon't Copy Locale on Compact=Fa" & _
    "lse;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"

    It makes me cannot open my database file.

    Why and how is this happening? It's so weird.

    How can i stop it from changing without my notice?

    Please guide. Thank you.

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    They look completely different ! What have you done with it ?

  10. #10
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    It happens to me to and I think the reason is you have removed the connection from Server Explorer. Try not deleting the connection in Server Explorer in IDE or if deleted already add it again and see if it happens.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width