Getting 'Could Not Find File' error when trying to update an Access database

hellsgate

Member
Joined
Apr 21, 2007
Messages
14
Programming Experience
1-3
Hi guys,I'm building an application which will hold details of my CD collection. One of the features is for a JPEG of the album cover to show whenever I select an album. In cases where I don't have a .jpg of the cover, a link label is enabled to allow me to get one from the 'net. When I've saved this onto the hard drive, I use an OpenFile dialog to browse to the picture location and display it in a picture box. At this point I want to update the database with the full path to the .jpg file using the following code:

VB.NET:
Dim SQL As String
 Dim UpdateCommand As OleDb.OleDbCommand
  
 SQL = "UPDATE music.albums SET CoverImageLocation = '" +  ImageFile + "' WHERE AlbumName = '" + AlbumName + "'"
 UpdateCommand = New OleDb.OleDbCommand(SQL,  OleDbConnection1)
 OleDbDataAdapter1.UpdateCommand = UpdateCommand
 OleDbDataAdapter1.Update(Music11, "albums")
ImageFile is taken from dlgOpen.FileName and in my test case is set to C:\Documents and Settings\Brian\My Documents\Visual Studio 2005\Projects\Class 10\Exercise 10\Exercise 10\bin\Debug\Covers\Master Of Puppets.jpg

This is where I've hit my problem. For this application, I've put the .mdb file into the DataDirectory, so the full path to the file is C:\Documents and Settings\Brian\My Documents\Visual Studio 2005\Projects\Class 10\Exercise 10\Exercise 10\music.mdb. However, when the application tries to update the database, the full error message I'm given is
Message="Could not find file 'C:\Documents and Settings\Brian\My Documents\Visual Studio 2005\Projects\Class 10\Exercise 10\Exercise 10\bin\Debug\Covers\music.mdb'."
This indicates to me that the application is now looking for the .mdb file in the last directory opened with the OpenFile dialog. If I move a copy of the .mdb file into this directory, the application successfully updates the required field, so I know the code itself is working properly. What I don't know is:
1. Why is the path to the Access file being changed?
2. How can I stop the path being changed?
If anyone requires any additional information on this, just ask and I'll be happy to supply it.
Any help you can give will be much appreciated.
 
Aside from the fact that we dont use + to concatenate strings in VB.NET, we never, ever, ever use string concatenation to build SQL queries. For a detailed explanation as to why this is completely the wrong way to make a database do something, read the PQ link in my signature.

And, given that youre using ADO.NET 2.0, you dont use this method to update databases any more. Have a read of the DW2 link in my signature, but start with the section on "Creating a simple app" then read "filling a dataset" or "displaying data on a form", then read "saving data"

If the menu on the left doesnt show for you, drag the blue bar
 
Hey cjard. As you'd probably expect, those links really helped out. I now have the app updating my database whenever I need it to. Thanks very much for pointing me in the right direction.
 
Back
Top