I need to save pictures in my database, my database is MSSQL
as of now, my code for getting the picture is this:

Private Sub browsepic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles browsepic.Click
With dialogpic 'Open File Dialog sa toolbox
.InitialDirectory = "C:\Users\CAMILLE\Pictures"
.Filter = "JPG Files|*.jpg|PNG Files|*.png|GIF Files|*.gif"
.FilterIndex = 2
End With

If dialogpic.ShowDialog() = DialogResult.OK Then
With studentpic
.Image = Image.FromFile(dialogpic.FileName)
.SizeMode = PictureBoxSizeMode.StretchImage
.BorderStyle = BorderStyle.Fixed3D
End With
End If
End Sub

Can you help me on how to save the picture and how to retrieve it?

thank you, i'll try it now and hope this works.

:)

Edited 4 Years Ago by aishapot: accident comment

I tried this code from the site you suggested.

Dim filename As String = txtName.Text + ".jpg"
Dim FileSize As UInt32

conn.Close()

Dim mstream As New System.IO.MemoryStream()
PbPicture.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim arrImage() As Byte = mstream.GetBuffer()

FileSize = mstream.Length
Dim sqlcmd As New MySqlCommand
Dim sql As String
mstream.Close()

sql = "insert into [your table] (picture, filename, filesize)
VALUES(@File, @FileName, @FileSize)"

Try
conn.Open()
With sqlcmd
.CommandText = sql
.Connection = conn
.Parameters.AddWithValue("@FileName", filename)
.Parameters.AddWithValue("@FileSize", FileSize)
.Parameters.AddWithValue("@File", arrImage)

.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try

When i tried to save the pic the error said The parameter data type of UInt32 is invalid What do i do?

Now it says String or binary data would be truncated. The statement has been terminated

Ok two questions:

1. sql = "insert into [your table] (picture, filename, filesize) VALUES (@File, @FileName, @FileSize)"
Is the table name really "your table"???

2. Could you please post the column definitions of your table?

This would help you convert the image in your picture box to binary and save it in sql server.

'//declarations to handle face capture and its conversion to binary

   Dim myfilelocation As String = lblfilePath.Text
   Dim param As New SqlParameter("@ImageData", SqlDbType.VarBinary)
   Dim ImageData As Byte() = IO.File.ReadAllBytes(myfilelocation)
   param.Value = ImageData

  '//end declaration 


  Dim dc As SqlCommand = New SqlCommand

      With dc
      .Connection = con  'assuming 'con' is your connectionstring 
      .CommandText = "INSERT INTO TableName" & _
           "(ImageColumn)" + _
              "VALUES (@imagedata)"

                .Parameters.Add(param)
      End With

	con.Open()
        dc.ExecuteNonQuery()
        con.Close()

Edited 4 Years Ago by Netcode: n/a

Ok two questions:

1. sql = "insert into [your table] (picture, filename, filesize) VALUES (@File, @FileName, @FileSize)"
Is the table name really "your table"???

2. Could you please post the column definitions of your table?

no. i changed that into the table that i'm using. i'm just pasting the code that someone suggested

This would help you convert the image in your picture box to binary and save it in sql server.

'//declarations to handle face capture and its conversion to binary

   Dim myfilelocation As String = lblfilePath.Text
   Dim param As New SqlParameter("@ImageData", SqlDbType.VarBinary)
   Dim ImageData As Byte() = IO.File.ReadAllBytes(myfilelocation)
   param.Value = ImageData

  '//end declaration 


  Dim dc As SqlCommand = New SqlCommand

      With dc
      .Connection = con  'assuming 'con' is your connectionstring 
      .CommandText = "INSERT INTO TableName" & _
           "(ImageColumn)" + _
              "VALUES (@imagedata)"

                .Parameters.Add(param)
      End With

	con.Open()
        dc.ExecuteNonQuery()
        con.Close()

ok i'll try it. thank you i hope it works

It sure works, just make all necessary substitutions correctly (e.g. your connection-string, table-name, column-name)

Please give feedback

This question has already been answered. Start a new discussion instead.