Results 1 to 5 of 5

Thread: [RESOLVED] OpenFileDialog messes up Database

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Resolved [RESOLVED] OpenFileDialog messes up Database

    First off, I'm fairly new to databases. I have a basic one that adds simple text...

    The problem I am running into is the OpenFileDialog box. I am allowing the user to pick an image and I want to save the image location into a column in the database(this isn't the problem), I know how to get the image location and save it...The main problem is this. Each time I open up the "OpenFileDialog box" and choose an image, it remembers the last location used, but after the image is chosen, the program looks at the images location as the spot the database is...

    To sum this up: Each time I use the "OpenFileDialog" and choose an image, the program looks at the images location for the database instead of in the "Debug folder" (which is were I put the database) So i end up getting an error that it can't find the database. Everything usually worked fine until I used the "OpenFileDialog"...

    thanks for any help

  2. #2
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: OpenFileDialog messes up Database

    How do you define the connection string?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: OpenFileDialog messes up Database

    Set the RestoreDirectory property of the OpenFileDialog to True or else specify the location for your database in such a way that doesn't rely on the current directory. That said, even if you do take the first option you should be taking the second anyway. How are you specifying the location of the database at the moment?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Re: OpenFileDialog messes up Database

    here is the connection string...

    variables:

    Dim ds As New DataSet()
    Dim intCurrentIndex As Integer = 0
    Dim da As New OleDbDataAdapter()
    Dim conn As New OleDbConnection()
    Dim intNum As Integer

    connection:

    conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = UserData.mdb;User Id=admin;Password=;"

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: OpenFileDialog messes up Database

    The fact that you're specifying only the database file name and not the path means that the path is assumed to be the application's current directory. When you app starts up its current directory will be the folder containing the EXE by default. When you display an OpenFileDialog, the current directory of the app follows wherever the user navigates. When they close the dialogue, the current directory is restored to its original location IF RestoreDirectory is True. In your case it's not so the current directory remains the folder containing the file that was opened.

    As I said before, you might want to set RestoreDirectory to True but, even if you do, you should still change your connection string. You should NEVER rely on the current directory unless you specifically want to follow the current directory even if it changes. If you want the folder containing the EXE then you should specify that folder. Your connection string should be:
    Code:
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source = |DataDirectory|\UserData.mdb;User Id=admin;Password=;
    The |DataDirectory| will resolve to the correct location whether you're debugging or you've deployed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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