0

I'm trying to save a picturebox into an ole database.

Here's my code :

Dim stream As New IO.MemoryStream
PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)
Try
    Dim query As String = "INSERT INTO Guestinfo ([GuestName],[Phone],[Idofguest],[Room],[Arrival],[Checkout],[Address],[IDImage]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox5.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "','" & TextBox4.Text & "',(@IDImage))"
    Dim command As New OleDbCommand

    With command
        .CommandText = query
        .Parameters.AddWithValue("@Picture", stream.GetBuffer())
        .Connection = conn
        .ExecuteNonQuery()
    End With
    MsgBox("Saved Successfully!", MsgBoxStyle.Information)
    conn.Close()
Catch ex As Exception
    MsgBox(ex.Message)
End Try

In "A Generic Error occurred in GDI+" this error appear:

PictureBox1.Image.Save(stream, Imaging.ImageFormat.Jpeg)

By the way, my column type is Ole Object. I hope someone help me ...

CodingSource
  • 203
  • 2
  • 15
Ramy Khaled
  • 15
  • 1
  • 7
  • 1
    There are several problems with that code, is there an image assigned to the PicBox? You also should dispose of the MemStream, you need to reset the stream.Positiom before trying to read from it, use ToArray not GetBuffer and use SQL Parameters: http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i – Ňɏssa Pøngjǣrdenlarp Dec 30 '14 at 02:03
  • Yes, there is an image assigned to the picbox, and for the dispose and reset i've just added it now. – Ramy Khaled Dec 30 '14 at 02:15
  • i wish for more help @Plutonix – Ramy Khaled Dec 30 '14 at 02:19

1 Answers1

0

This application has an Open Button which will help you open any picture file to a PictureBox on the form using OpenFileDialog. You will see the path of the picture file in a disabled TextBox. When you click the update button the Picture's path is saved to an Access Database.

Follow the steps below to create a similar project for yourself: * Create a new Visual Basic.net project. Select Windows Forms Application from New Project Dialog Box. Name this application whatever you want. * Create the following with below mentioned properties: - Form - (Name): sample, Text: FormPictureApplication - PictureBox - (Name): PictureBox1, SizeMode: StretchImage - Button - (Name): ButtonUpdate, Text: &Update - Button - (Name): ButtonOpen, Text: &Open - TextBox - (Name): TextBoxPictureFilePath, Enabled: False

  • Double Click the Form, insert the following code right above Public Class {...}:

    Imports System.Data.OleDb Imports System.IO Imports Microsoft.Win32

    Double Click ButtonOpen and insert the following code:

    Dim img As String

    Dim myStream As Stream = Nothing Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\" openFileDialog1.Filter = Nothing openFileDialog1.FilterIndex = 2 openFileDialog1.RestoreDirectory = True openFileDialog1.FileName = ""

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Try myStream = openFileDialog1.OpenFile() If (myStream IsNot Nothing) Then

    TextBoxPictureFilePath.Text = ""

    img = openFileDialog1.FileName PictureBox1.Image = System.Drawing.Bitmap.FromFile(img)

    TextBoxPictureFilePath.Text = openFileDialog1.FileName

    End If Catch Ex As Exception MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message) Finally If (myStream IsNot Nothing) Then myStream.Close() End If End Try End If

  • Create a Microsoft Access Database in your convenient location and name it as Databasemikeoe2003PictureApplication.mdb

  • Create a table with the name Tablemikeoe2003PictureApplication and add following Columns to it:

    Id - Datatype: Autonumber PicturePath - DataType: Memo (as file paths can be considerably long at times)

  • Double Click the UpdateButton and insert the following code:

    Try Dim myConnection As OleDbConnection Dim myCommand As OleDbCommand Dim mySQLString As String myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Databasemikeoe2003PictureApplication.mdb;") myConnection.Open() mySQLString = "INSERT INTO Tablemikeoe2003PictureApplication (PicturePath) VALUES('" & Replace$(TextBoxPictureFilePath.Text, "'", "''") & "')" myCommand = New OleDbCommand(mySQLString, myConnection) myCommand.ExecuteNonQuery()

    PictureBox1.Image = Nothing TextBoxPictureFilePath.Text = ""

    Catch ex As Exception MessageBox.Show(ex.Message & " - " & ex.Source) End Try

  • Run the application, it should work as desired.

CodingSource
  • 203
  • 2
  • 15