Results 1 to 2 of 2

Thread: saving images to database

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    1

    saving images to database

    Hi,
    This is a newbie question. I am trying to create an applicatiojn which allows the user to enter info on plants including photos. I have set up the database using image datatype and created the form using the dataset but when I save the data everything is saved but the image file. Obviously I need extra code but I can't find anything in the help files etc that shows me. This is the code I have at the moment

    Imports System.IO

    Public Class NewTree
    Dim openFileDialog1 As New OpenFileDialog()
    Dim fileName As String

    Private Sub TreesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreesBindingNavigatorSaveItem.Click
    Me.Validate()
    Me.TreesBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.LibraryDataSet)

    End Sub

    Private Sub NewTree_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'LibraryDataSet.Trees' table. You can move, or remove it, as needed.
    'Me.TreesTableAdapter.Fill(Me.LibraryDataSet.Trees)

    End Sub
    'Browse for image to be stored in data base
    Private Sub ImageBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageBtn.Click
    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()
    Dim fileName As String


    With openFileDialog1
    .InitialDirectory = "C:\" 'set initial drive for browsing
    .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"
    .FilterIndex = 4 'set first type of files


    End With
    Try
    If openFileDialog1.ShowDialog() = DialogResult.OK Then 'opens dialog box
    fileName = openFileDialog1.FileName 'gets file name
    With ImagePictureBox
    .Image = Image.FromFile(fileName) 'sets image into picture box
    .SizeMode = PictureBoxSizeMode.StretchImage 'sets image size

    End With

    End If
    Catch ex As Exception
    MsgBox(ex.Message)


    End Try
    ' Check this again, since we need to make sure we didn't throw an exception on open.
    If (myStream IsNot Nothing) Then
    myStream.Close()
    End If


    End Sub

    Please if any one can help me -

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

    Re: saving images to database

    Follow the CodeBank link in my signature and you'll find my submission on the subject.
    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