Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open File Dialog and Database Location Problem

Status
Not open for further replies.

anthony1709

Programmer
Nov 14, 2006
6
GB
Hi,

I have come across a problem which I'm unable to solve.

I'm creating a VB.Net application using Visual Studio 2005. On one of my forms, I've save a new record/row to my database. On this form, it also has a Open File Dialog component, which opens a picture, so that I can save the picture to the database. The problem occurs whenever I use the Open File Dialog, and open a picture, it comes up with the error saying it can't find my database. It shows me the path is it looking for it, and its the wrong path.

But when I don't use the Open File Dialog, it works perfectly fine. It saves the new record/row to the database. Its whenever I open a file using the Open File Dialog that it doesn't work.

If I hit the cancel button on the Open File Dialog it will still work, its whenever I open a file that it comes up with the error of not finding my database, as it is looking in the wrong directory.

I tried this in a new project and the same thing happens. It seems that whenever I open a file using the Open File Dialog, it is changing the path of where my database is, to the path of where the file I've just opened.

I have no idea why this is happening, please help me.

Here is my code for my database connection:

'Connection to the Access database
Public Const strConnection As String = _
Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=./myDatabase.mdb;Persist Security Info=False"

and here is my code for my Open File Dialog:

OpenFileDialog.Filter = "Image files (.jpg, .jpeg, .bmp, .gif)|*.jpg;*.jpeg;*.bmp;*.gif"
If (OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
imageFileName = OpenFileDialog.FileName
txtImage.Text = imageFileName
End If

Thanks very much, any help much appreciated.
 
Code:
./myDatabase.mdb

seems like a relative path to me. If I'm not mistaken, the OpenFileDialog will override current path which affects that relative path.

Try putting an absolute path to it, for instance:
Code:
Public Const strConnection As String = _
Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=" & Application.StartupPath & "\myDatabase.mdb;Persist Security Info=False"

Hope this helps
 
Hi,

Thanks very much for your help and quick reponse.

Yes you are the OpenFileDialog will override my current path. So I have change the my path to the database as:

"Data Source=|DataDirectory|/myDatabase.mdb"

But I'm sure you way would work as well.

Again thanks very much,

Anthony.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top