Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Problem saving image path to db

  1. #1

    Thread Starter
    Junior Member maco's Avatar
    Join Date
    Aug 2006
    Location
    Linköping, Sweden
    Posts
    20

    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.  
    3.         With OpenFileDialog1.Filter = "All Images (*.gif; *.jpeg; *.jpg; *.bmp)|*.gif; *.jpeg; *.jpg; *.bmp"
    4.         End With
    5.         If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.  
    7.             PictureBox3.SizeMode = PictureBoxSizeMode.StretchImage
    8.             PictureBox3.ImageLocation = OpenFileDialog1.FileName
    9.  
    10.  
    11.             BrowseText.Text = OpenFileDialog1.FileName
    12.         End If
    13.  
    14.  
    15.  
    16.  
    17.     End Sub



    vb Code:
    1. Private Sub AddMovieBtn_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddMovieBtn.Click
    2.  
    3.         Dim addresponce As Integer
    4.         addresponce = MsgBox(" Do you want to add the following object? " + "Movie Title: " + Title.Text, MsgBoxStyle.YesNo)
    5.         If addresponce = vbYes Then
    6.  
    7.  
    8.             If Title.Text = "" And Actor.Text = "" And Descrip.Text = "" And Length.Text = "" Then
    9.  
    10.  
    11.  
    12.                 MsgBox("You have to fill at least 1 field!")
    13.             Else
    14.  
    15.                 If Rating.Text = "" Then
    16.                     Rating.Text = "1"
    17.                 End If
    18.  
    19.                 If Length.Text = "" Then
    20.                     Length.Text = "1"
    21.                 End If
    22.  
    23.  
    24.  
    25.  
    26.                 Try
    27.  
    28.  
    29.  
    30.                     cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=movies.mdb;")
    31.                     cn.Open()
    32.                    
    33.                     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 & "')"
    34.                    
    35.  
    36.                     cmd = New OleDbCommand(str, cn)
    37.                     icount = cmd.ExecuteNonQuery
    38.                     'MsgBox(icount)
    39.                     'displays number of records inserted
    40.  
    41.                     cn.Close()
    42.  
    43.  
    44.  
    45.  
    46.  
    47.  
    48.  
    49.                     MsgBox("The Movie Has Been Added To The Database")
    50.  
    51.                     'Rensa ut all data
    52.                     Title.Text = ""
    53.                     Length.Text = ""
    54.                     Actor.Text = ""
    55.                     Descrip.Text = ""
    56.                     Rating.Text = ""
    57.                     Genre.Text = ""
    58.                     FileLocation.Text = ""
    59.                     BrowseText.Text = ""
    60.  
    61.  
    62.                     PictureBox3.Image = Nothing
    63.  
    64.  
    65.  
    66.                 Catch ex As Exception
    67.                     MessageBox.Show(ex.ToString())
    68.  
    69.                     'Catch
    70.                     '   MsgBox("An error was reported! Please try again!")
    71.                 End Try
    72.  
    73.  
    74.  
    75.  
    76.  
    77.  
    78.             End If
    79.  
    80.         Else
    81.             If addresponce = vbNo Then
    82.                 Me.Select()
    83.             End If
    84.         End If
    85.         Me.MoviedataTableAdapter.Fill(Me.MoviesDataSet.moviedata)
    86.  
    87.  
    88.     End Sub

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

    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.
    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

  3. #3

    Thread Starter
    Junior Member maco's Avatar
    Join Date
    Aug 2006
    Location
    Linköping, Sweden
    Posts
    20

    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.


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

    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.
    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

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