Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble inserting image into DB

Status
Not open for further replies.

SavantMan

Programmer
Apr 17, 2002
165
US
I have a stored procedure that is intended to simply insert an image into a DB. The data back end is MSDE. When the procedure executes, it returns: "Failed to convert parameter value from a Bitmap to a Byte[]."

Anyone have any idea what might be wrong?

Code:
   ''' <summary>Inserts a new object into the datastore</summary>
   Private Sub InsertNewObject()

      Dim cn As New SqlConnection
      cn.ConnectionString = "" '<connection string goes here>
      cn.Open()

      Try
         Using cm As SqlCommand = cn.CreateCommand
            cm.CommandType = CommandType.StoredProcedure
            cm.CommandText = "BackgroundImageInsert"
            cm.Parameters.AddWithValue("@ID", Me.mID)
            Dim ImageParam As New SqlClient.SqlParameter("@BackgroundImage", SqlDbType.Image)
            ImageParam.Value = Me.mImage
            cm.Parameters.Add(ImageParam)
            cm.ExecuteNonQuery()
         End Using
      Catch ex As Exception
         Debug.WriteLine("Insert BackgroundImageItem Failed")
      Finally
         cn.Close()
      End Try

   End Sub
 
My guess is that you need to convert you image to a byte and store that in the DB.
 
you'll have to convert the image into a byte array and pass the byte array to your stored proc. here the code for converting an image into byte array

Dim ms As New System.IO.MemoryStream
myImage.Save(ms, myImage.RawFormat)

Dim byteArray() As Byte = ms.GetBuffer()
ms.Close()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top