To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 20th, 2007, 03:14 AM   #1
maco
Junior Member
 
maco's Avatar
 
Join Date: Aug 06
Location: Linköping, Sweden
Posts: 20
maco is an unknown quantity at this point (<10)
Resolved [RESOLVED] [2005] Problem saving image path to db

Hi guys!

I'm using an openfiledialog along with some other textboxes and so and then extracting the text from the textboxes and putting them into my database. When I open a file with the dialog I also take the path to the file and put it in to a textbox to save it later in the database. But this is where it all goes wrong, it works fine to save if I don't import a picture, but once I import a picture (it goes in a picture box for preview and path in the textbox) then when I add the stuff to the database it changes the path to the database, replacing it with the path to the image? :S

And I just can't figure out why! I've tried everything, I tried writing the path to a textfile instead, same error, even when that code is outside the db-connection.

Even if I don't put in any code relating to saving the image in the database or anywhere else I still get the same error as long as I import an image to my picturebox!

I really need help!

Here's the code for importing the image, and saving it to my database:


vb Code:
  1. Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         With OpenFileDialog1.Filter = "All Images (*.gif; *.jpeg; *.jpg; *.bmp)|*.gif; *.jpeg; *.jpg; *.bmp"
  3.         End With
  4.         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  5.             PictureBox3.SizeMode = PictureBoxSizeMode.StretchImage
  6.             PictureBox3.ImageLocation = OpenFileDialog1.FileName
  7.             BrowseText.Text = OpenFileDialog1.FileName
  8.         End If
  9.     End Sub



vb Code:
  1. Private Sub AddMovieBtn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddMovieBtn.Click
  2.         Dim addresponce As Integer
  3.         addresponce = MsgBox(" Do you want to add the following object? " + "Movie Title: " + Title.Text, MsgBoxStyle.YesNo)
  4.         If addresponce = vbYes Then
  5.             If Title.Text = "" And Actor.Text = "" And Descrip.Text = "" And Length.Text = "" Then
  6.                 MsgBox("You have to fill at least 1 field!")
  7.             Else
  8.                 If Rating.Text = "" Then
  9.                     Rating.Text = "1"
  10.                 End If
  11.                 If Length.Text = "" Then
  12.                     Length.Text = "1"
  13.                 End If
  14.                 Try
  15.                     cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=movies.mdb;")
  16.                     cn.Open()
  17.                    
  18.                     str = "INSERT INTO moviedata (Title, Length, Actors, Description, Rating, Genre, Location, ImageLocation) VALUES('" & Title.Text & "' , '" & Length.Text & "' , '" & Actor.Text & "' , '" & Descrip.Text & "' , '" & Rating.SelectedItem & "' , '" & Genre.SelectedItem & "  ' , '" & FileLocation.Text & "', '" & BrowseText.Text & "')"
  19.                    
  20.                     cmd = New OleDbCommand(str, cn)
  21.                     icount = cmd.ExecuteNonQuery
  22.                     'MsgBox(icount)
  23.                     'displays number of records inserted
  24.                     cn.Close()
  25.                     MsgBox("The Movie Has Been Added To The Database")
  26.                     'Rensa ut all data
  27.                     Title.Text = ""
  28.                     Length.Text = ""
  29.                     Actor.Text = ""
  30.                     Descrip.Text = ""
  31.                     Rating.Text = ""
  32.                     Genre.Text = ""
  33.                     FileLocation.Text = ""
  34.                     BrowseText.Text = ""
  35.                     PictureBox3.Image = Nothing
  36.                 Catch ex As Exception
  37.                     MessageBox.Show(ex.ToString())
  38.                     'Catch
  39.                     '   MsgBox("An error was reported! Please try again!")
  40.                 End Try
  41.             End If
  42.         Else
  43.             If addresponce = vbNo Then
  44.                 Me.Select()
  45.             End If
  46.         End If
  47.         Me.MoviedataTableAdapter.Fill(Me.MoviesDataSet.moviedata)
  48.     End Sub
maco is offline   Reply With Quote
Old Apr 20th, 2007, 03:41 AM   #2
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [2005] Problem saving image path to db

It's because of this:
Code:
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=movies.mdb;")
By not specifying the absolute path you are implicitly using the current directory. When you run your app the current directory will initially be the folder containing the executable by default, but it can change.

One of the things that can change it is an OpenFileDialog. A quick fix is to set the RestoreDirectory property of the OFD to True so it doesn't change the current directory, but that's not a proper solution.

There's no guarantee that something else won't change the current directory. Unless you specifically want to follow the current directory you shouldn't use a path that implies the current directory. If you want a path that refers to the folder that contains the executable then that's what you should use. In a .NET connection string that looks like this:
vb Code:
  1. cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\movies.mdb;")
The |DataDirectory| variable refers to the folder containing the executable unless you change it through the current AppDomain. That's not going to happen unless you specifically want it to.
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Old Apr 20th, 2007, 05:05 AM   #3
maco
Junior Member
 
maco's Avatar
 
Join Date: Aug 06
Location: Linköping, Sweden
Posts: 20
maco is an unknown quantity at this point (<10)
Re: [2005] Problem saving image path to db

Thanks Jim!

I solved it by creating a new string that contains the Application.Startuppath plus the db name. Then changing the source to that string.

maco is offline   Reply With Quote
Old Apr 20th, 2007, 08:56 AM   #4
jmcilhinney
.NUT
 
jmcilhinney's Avatar
 
Join Date: May 05
Location: Sydney, Australia
Posts: 61,544
jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)jmcilhinney has a reputation beyond repute (3000+)
Re: [RESOLVED] [2005] Problem saving image path to db

Why do that when you can do it "properly" with |DataDirectory|? That's specifically why the |DataDirectory| variable exists.
__________________

2007-2010

Why is my data not saved to my database? | Communicating between multiple forms | MSDN Data Walkthroughs
MSDN "How Do I?" Videos: VB | C#
VBForums Database Development FAQ
My CodeBank Submissions: VB (Nullable Data Extensions *NEW*) (Serial Code TextBox *NEW*) | C# (ForumAccount has translated some of my VB submissions to C#)
My Blog: Defining and Raising Custom Events | Using Parameters in ADO.NET | Keyboard Events *NEW*
jmcilhinney is online now   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:37 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.