שתף באמצעות


How Do I Save An Image in a Picture Box to SQL Database Image Field

Question

Thursday, February 3, 2011 11:28 PM

Using VB.net 2010, Windows 7, and SQL Server Express 2008 R2.

I am trying to save an image from a picture box to an image field in SQL Server.  The line of code below produces an error.  What am I doing wrong?

...code

objCommand.Parameters.AddWithValue("@ss_no", mskSS_No.Text)

objCommand.Parameters.AddWithValue("@d_code", txtD_Code.Text)

objCommand.Parameters.AddWithValue("@photo", picPhoto.Image)  < produces error

 

...more code

Any help would be appreciated.

 

All replies (4)

Friday, February 4, 2011 9:13 PM ✅Answered | 1 vote

Jeff,

Jeff in order to save to the photo you have to create a byte array of the image and pass it into the SavePhoto Method.  Here is an example of how to do it:

'- Create a new memory stream object 
Dim m As New IO.MemoryStream

'- Get the image from the picture control
 PictureBoxControl.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)

'- Convert the stream to the byte array (Blob for SQL)
 Dim b() As Byte = m.ToArray()

'- Call the save
SavePhoto(b())

Hope that helps.  Best of luck

Robert McArthur


Thursday, February 3, 2011 11:54 PM | 2 votes

Read this How To Read and Write BLOB Data by Using ADO.NET with Visual Basic .NET

kaymaf

CODE CONVERTER SITE

http://www.carlosag.net/Tools/CodeTranslator/.

http://www.developerfusion.com/tools/convert/csharp-to-vb/.


Friday, February 4, 2011 12:11 AM | 2 votes

Hello Jeff07,

1) Make sure the data type in SQL is image
2) Image Type in sql is acutally a Byte Array, so you store it in code as a byte()

Example of reading in from SQL

Dim oMemoryStream As New IO.MemoryStream(moEmployee.Photo)
picEmployeePhoto.Image = Image.FromStream(oMemoryStream)

For your isssue of saving:

Public Sub SavePhoto(ByVal bytPhoto As Byte())

   '-Code Here for connection object

   '-Code here for command object

   '- Code for Parameters
   oCommand.Parameters.Add("@photo", SqlDbType.Image, bytPhoto.Length).Value = bytPhoto

   '- Update the database
   oCommand.ExecuteNonQuery()

End Sub

Hope this helps you,

 

Robert McArthur

Robert McArthur


Friday, February 4, 2011 4:34 PM

I created a SavePhoto proceedure:

Public Sub SavePhoto(ByVal bytPhoto As Byte())

        Dim objCommand As SqlCommand = New SqlCommand

        objCommand.Connection = objConnection

        objCommand.CommandText = "UPDATE names " & _
                "SET photo = @photo " & _
                "WHERE account_no = @account_no"

        objCommand.CommandType = CommandType.Text

        objCommand.Parameters.Add("@photo", SqlDbType.Image, bytPhoto.Length).Value = bytPhoto

        Try
            objCommand.ExecuteNonQuery()
        Catch SqlExceptionErr As SqlException
            MessageBox.Show(SqlExceptionErr.Message)
        End Try
    End Sub

 

My newbie question is:  What goes in the parentheses of the SavePhoto() procedure.  My photo is in a picture box, picPhoto, and not in a file per se.

Thanks for taking the time to answer my question.